By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

Service stop timeout

P: n/a
I have two windows services running together. One is the main
program, and one is an "updater" wrapper for the main. The updater
service starts and stops the main one (as in you should never start or
stop main service manually, only updater).
On most computers it works great, the updater's onStop() can start and
stop the main service perfectly. On some computers, though, the
updater hangs when it tries to stop the main service. It acutally
does stop the service correctly, but it doesnt seem to be getting a
return telling it that the other service has stopped, so it sits there
until timeout.

protected override void OnStop() //for updater
this.eventLog1.WriteEntry("about to try stopping main");
updater.Run.StopService( ); //see below
this.eventLog1.WriteEntry("should all be stopped. how long did that

public static void StopService( )
SysLog logger = new SysLog();
logger.Log( "Stopping the Updater." );

//some other code goes here

StopMain(); //see below
logger.Log("main stopped");

private static bool StopMain()
SysLog logger = new SysLog();
logger.Log("stopping Main");
ServiceController sc = null;
/* Determine if an instance of Main is running. */
sc = new ServiceController("nameOfMainService");
sc.MachineName = ".";
Thread.Sleep( 2000 );
catch( Exception ex )
throw ex;
if ( sc.Status == ServiceControllerStatus.Running || sc.Status ==

logger.Log("in StopMain(): Main is started, need to stop it");
logger.Log( "sc.Stop()" );
logger.Log( "sc.Stop() done" );
catch(InvalidOperationException ex)
catch(Exception ex)
logger.Error( "Error stopping service. " + ex.ToString()
+ex.StackTrace );
throw ex;
return true;

//Main's onStop()
protected override void OnStop()
this.eventLog1.WriteEntry("stopping Main. This is where its hanging
// Insert code here to define processing.
Ok. So theres the relevant code. The wierd thing is this... if you
find in the StopMain() method, there are two logger lines one that
outputs "sc.stop()" and one that outputs "sc.stop() done". There is a
30 second delay between these two statements executing. This would
cause me to believe taht the hangup is in the onStop method for the
Main service. However, if you look at the onStop, the first thing it
does is output to the event log. This event log output is 30 seconds
after the logger.Log( "sc.Stop()" ); line. There is a 30 second gap
here, where i really dont know what is happening. So in summary,
there is a 30 second gap between the last thing the StopMain() does
before calling sc.stop() and the first thing the onStop() method does,
with nothing in between.

So, that was a lot. I hope that made sense. Any ideas what would be
causing that 30 second timeout there?

Oct 19 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.