Stop egen service
har følgende kode som eksikverer, men skal herefter stoppe den pågældende service. Den bliver startet af en bat fil men skal stoppe efter udførelsen.kode:
___________________________________________________________________
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net.Mail;
using System.Timers;
namespace smtpMdRepService
{
public partial class smtpRepMD : ServiceBase
{
public smtpRepMD()
{
InitializeComponent();
if(!System.Diagnostics.EventLog.SourceExists("RepLogSourse"))
System.Diagnostics.EventLog.CreateEventSource("RepLogSourse",
"RepLog");
eventLog1.Source = "RepLogSourse";
// the event log source by which
//the application is registered on the computer
eventLog1.Log = "RepLog";
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service
eventLog1.WriteEntry("The Replication of MD service was started without complcations");
SendEmail();
}
private void SendEmail()
{
MailMessage message = new MailMessage();
message.From = new MailAddress("rep@muhldorff.dk");
message.To.Add(new MailAddress("support@techbiz.dk"));
System.DateTime dt = System.DateTime.Now;
string sdt = dt.ToString("dd--MM-yyyy-hh-min");
message.Subject = "Rep Ok" + " " + sdt;
message.Body = "Replikeringen blev udført uden komplikationer";
message.IsBodyHtml = false;
message.Priority = MailPriority.Normal;
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.Host = "mail.techbiz.dk";
mSmtpClient.Port = 25;
mSmtpClient.Send(message);
ServiceController sc = new ServiceController("smtpRepMD");
sc.Stop();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
eventLog1.WriteEntry("The service smtpRepMD has stopped");
}
protected override void OnContinue()
{
eventLog1.WriteEntry("The service started again!");
}
public void stopService()
{
ServiceController sc = new ServiceController("smtpRepMD");
sc.Stop();
}
}
}