Solved

Service Onstart OnStop

Posted on 2004-10-14
13
957 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
  • 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now