John Ryann
John Ryann

Reputation: 2393

C# windows services program

I was able to create a simple windows service app. just the frame. but I am still confused. where should I put my code for the windows service to actually do something. I have a separate program that I would like to include/call/incorporate here. where should put the program? where should I start?

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);
    }
}

}

Upvotes: 3

Views: 2801

Answers (7)

djSmart
djSmart

Reputation: 113

Override the OnStart() Method to have your business logic invoked. As mentioned earlier, you may either create another thread to have the functionality or use Eventhandlers with Timers (using threading again) to have the busineess logic invoked. The Service must return control to the OS and hence the Onstart Method should return the control to windows while the service is running.

To control , Pause, Resume, on Power Shutdown, On Stop events, you need to override these methods and write your logic there.

Upvotes: 0

Corwin01
Corwin01

Reputation: 479

As has been said, don't put your code in OnStart(). Why? Because if your OnStart() method doesn't return quickly, the service manager is going to flag your service as unresponsive, and will shut you down.

So, I put my code in a Start() method, and all OnStart() does is call Start(). Like so:

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

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

Also, be aware that your code to start the service is going to behave different 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 remember 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);
        }

Upvotes: 0

Felice Pollano
Felice Pollano

Reputation: 33242

You must override the OnStart() function. I suggest to move all the code you currently have in the constructor in the same function since it is recommended to leave the constructor empty ( this not just for services, but this is another story ). In the start you usually spin one or more thread doing the job you want. Remember the OnStart() must return as far as possible. You probably need to implement some logic in the OnStop() function too, in order to gracefully block the working threads.

Upvotes: 0

Jason
Jason

Reputation: 6926

In this walkthrough, it says you override OnStart().

If you're not tied to using that Windows Service program template, you may want to check out this library which makes programming services much much easier. Right now, the service executable you build can't directly be run - it can only be installed. Hoytsoft's library installs and then automatically runs it for you after, just like a normal Windows Form application.

Upvotes: 1

Guvante
Guvante

Reputation: 19203

You react to the various events sent by using the On____ methods (they are virtual so you can override them).

Specifically in the simplest case:

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

Upvotes: 0

Erik Philips
Erik Philips

Reputation: 54618

Override the following Methods on your MyNewService as needed:

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);

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564323

You need to override the OnStart method (and other similar ones, such as OnStop, OnShutdown, etc).

When you do this, make sure that your OnStart method does not block or take very long to execute. This often means running your actual service logic in its own thread.

Upvotes: 3

Related Questions