Click here to Skip to main content
15,909,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a windows service. In OnStart(), I check license first. If the license is not there, the service stops. I have tried several different ways to do that, such as throw an exception. It works. The only problem is that it pops up a dialog saying "service on local computer started then stopped. xxxxx" and user need to click OK to discharge the message dialogue.

Is there a way to stop the service w/o any front-end pop-ups?
Posted
Updated 8-Nov-10 10:28am
v2

1 solution

You need to add a delay before stopping the service when it is starting. Hopefully you have most of the service functionality in another class and the ServiceBase is just a shell in which case you can have something like below.

C#
protected override void OnStart(string[] args)
{
    if(licenceIsValid)
    {
        _realService.Start();
    }
    else
    {
        ThreadPool.QueueUserWorkItem(delegate
            {
                Thread.Sleep(TimeSpan.FromSeconds(10));
                Stop();
            });
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900