C # window service program

I was able to create a simple Windows application. just a frame. but I am still confused. where should i put my code for windows service to actually do something. I have a separate program that I would like to include / enable / include here. where should the program be delivered? where to begin?

public partial class MyNewService : ServiceBase
{
    public MyNewService()
    {
        InitializeComponent();
        if (!System.Diagnostics.EventLog.SourceExists("MySource"))
        {
            System.Diagnostics.EventLog.CreateEventSource(
                "MySource", "MyNewLog");
        }
        eventLog1.Source = "MySource";
        eventLog1.Log = "MyNewLog";
    }




    static void Main()
    {
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        // Change the following line to match.
        ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyNewService() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}

      

}

+3


source to share


7 replies


You need to override the OnStart method (and others like it, for example OnStop

, OnShutdown

etc.).



When you do, make sure that your method is OnStart

not blocking or taking a very long time. This often means using your actual service logic in your thread.

+3


source


In this walkthrough , it says you are overriding OnStart()

.



If you are not tied to using this Windows Service Program Template, you may need to check out this library , which makes Easy Programming Services much easier. Right now, the service executable you create cannot be run directly - it can only be installed. The Hoytsoft library is installed and then automatically launches it for you like a normal Windows Form application.

+1


source


Change the following methods to MyNewService

:

protected virtual void OnContinue();
protected virtual void OnCustomCommand(int command);
protected virtual void OnPause();
protected virtual bool OnPowerEvent(PowerBroadcastStatus powerStatus);
protected virtual void OnSessionChange(SessionChangeDescription changeDescription);
protected virtual void OnShutdown();
protected virtual void OnStart(string[] args);

      

0


source


You react to various events dispatched via methods On____

(they are virtual, so you can override them).

In particular, in the simplest case:

protected override void OnStart(string[] args)
{
    //Do stuff here
}

      

0


source


You must override the function OnStart()

. I suggest moving all the code you have in the constructor in the same function, as it is recommended to leave the constructor empty (this is not only for services, but this is another story). In the beginning, you usually include one or more threads to do the work you want. Remember, OnStart () should return as far back as possible. You may need to implement some logic in the OnStop () function to gracefully block worker threads.

0


source


As already said, don't put your code in OnStart (). What for? Because if your OnStart () method doesn't return quickly, the service manager will mark your service as unresponsive and disconnect you.

So, I put my code in the Start () method, and all OnStart () is a Start () call. For example:

    protected override void OnStart(string[] args)
    {
        Start();
    }

    public static void Start()
    {
       ... do stuff
    }

      

Also, keep in mind that your code to start the service will behave differently depending on whether you are in compile or debug mode.

System.ServiceProcess.ServiceBase[] ServicesToRun; // Change the following line to match. ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyNewService() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun);

I do this to make sure it behaves correctly and I don't have to forget to change the code back and forth to run or debug.

        if(Debugger.IsAttached)
            Service.Start();
        else
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                new Service() 
            };

            ServiceBase.Run(ServicesToRun);
        }

      

0


source


Override the OnStart () method to call your business logic. As mentioned earlier, you can create another thread to have the functionality, or use Eventhandlers with timers (with thread repeating) to activate the logic of business processes. The service must return control to the OS and therefore the Onstart method must return the control to the windows while the service is running.

To manage, suspend, resume, power off, enable stop events, you need to override these methods and write your logic there.

0


source







All Articles