Solved

Service Onstart OnStop

Posted on 2004-10-14
13
964 Views
Last Modified: 2008-03-10
I have created a service in C#

in the Onstart function
i would like to be able to stop the service if there is an error,
but the service does not stop.

Onstart()
{
    // code

    onstop()

}

How can I force the service to stop in Onstart?
0
Comment
Question by:doudou74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 22

Expert Comment

by:cookre
ID: 12311136
Just do an Application.Exit()

The reason it didn't stop is that the service per se had not yet been placed in the started mode.

OnStart() is what the OS triggers when the service's START command is issued.  The service is not considered started until its status is set to RUNNING.  While in OnStart(), c# keeps the status as PENDING until OnStart() is successfully exited.
0
 

Author Comment

by:doudou74
ID: 12316930
can i do an Application.Exit() in a service?? Is not is only for forms?
what should I include as a reference or namespace?

0
 
LVL 22

Expert Comment

by:cookre
ID: 12320500
Yes, that's what I do in my c# services - works fine.

using System.Windows.Forms;
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:doudou74
ID: 12321234
Onstart()
{
    // code

    Application.Exit()
}

I tried this but the service still appears as started in the list of services

0
 
LVL 22

Expert Comment

by:cookre
ID: 12321744
I would guess either there was an instance already running, or the OnStart() exited somewhere without hitting the Exit().

Take a look at the Application and System event logs to see if there is anything interesting there.
0
 

Author Comment

by:doudou74
ID: 12329207
i checked all this. it does not work
0
 
LVL 22

Expert Comment

by:cookre
ID: 12329309
Looks like you'll have to post the code...
0
 

Author Comment

by:doudou74
ID: 12336750
here is my code

protected override void OnStart(string[] args)
{
    // TODO: Add code here to start your service.

    Application.Exit()
}


protected override void OnStop()
{
  // TODO: Add code here to perform any   tear-down necessary to stop your service.
}
0
 

Author Comment

by:doudou74
ID: 12336887
here is my code

protected override void OnStart(string[] args)
{
    // TODO: Add code here to start your service.

    Application.Exit();
}


protected override void OnStop()
{
  // TODO: Add code here to perform any   tear-down necessary to stop your service.
}
0
 
LVL 22

Expert Comment

by:cookre
ID: 12353872
There are conditions under which .Exit() won't, such as hanging message boxes.

Surely there's more going on that just what was posted.
0
 
LVL 3

Expert Comment

by:mpf1748
ID: 12384394
There is no way to stop a process during the OnStart event. Create a global bool variable bError and set it to true if there is an error. Then start a timer on the very last line of code of the OnStart for say 5 milliseconds. In the event handler for the timer, check to see if bError is true. If so, Stop, else nothing. Be sure and disable the timer.

HTH,
Matt
0
 

Author Comment

by:doudou74
ID: 12384799
-what is the function to call to stop the service?
-can you give me an example of your idea of timer
0
 
LVL 22

Accepted Solution

by:
cookre earned 125 total points
ID: 12385267
// global
System.Timers.Timer INITimer;



protected override void OnStart(string[] args)
{
...
INITimer.Elapsed+=new ElapsedEventHandler(INITimer_Elapsed);
INITimer.Interval=1000; // or whatever
INITimer.Enabled=true;
}


void INITimer_Elapsed(object source,ElapsedEventArgs evt)
{
// Disable timer so we don't interrupt ourselves
INITimer.Enabled=false;

// Do things
...

}
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question