Create windows service

Description :


We can create the windows service by using the following code.

Namespace part

using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;



Coding part

public class MyWinService : System.ServiceProcess.ServiceBase {
private EventLog MyeventLog;

public MyWinService() {
this.ServiceName = "MyWinService";

string Mysource = "Main";
MyeventLog = new EventLog();
MyeventLog.Source = Mysource;
}
static void Main() {
System.ServiceProcess.ServiceBase[] MyServicesToRun;
MyServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyWinService() };
System.ServiceProcess.ServiceBase.Run(MyServicesToRun);
}
protected override void OnStart(string[] args) {
MyeventLog.WriteEntry("Service starting ..!");
}
protected override void OnStop() {
MyeventLog.WriteEntry("Service shutting down..!");
}
}


Code Explanation

1. Create the ServiceProcess
2. Assign the service to run
3. Write the event log once the service start
4. Write the event log once the service stop


By
Nathan


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: