I developing an application based on some windows services on .NET framework
1.1. Eatch service are a single process service. Each service connects to
SQL Server or Oracle database and derives an customiced TCP server class. I
notised that the SCM calls the OnShutdown method when the system shutsdown
and the OnStop method at service stop from SCM. The service stop from SCM
calls the OnStop with different thread than the Main function and the
service stops in a controlled way after the OnStop returns but the
OnShutdons is called with same thread as the Main during the shutdown or
restart cycle and the process will not be stopped in a controlled way. All
database connections in the application database connection pool will remain
open in the Oracle servcers point of view. This cause an out of available
connections for the services after system reboot. I created a clean service
to make shure that no external thread is locked and added some tracings
shown in the code below. I also attached the output from the trace file. I
really hope that someone find some wrong in my code.
-----------------------
Stefan S
/Thanks on advance
The Threadid are obtained with the Thread.GetHashCode() method.
15:04:09, ThreadId:2 Main Called
15:04:09, ThreadId:5 OnStart Called, and Returns
15:04:17, ThreadId:5 OnStop Called
15:04:17, ThreadId:5 StopService Called
15:04:17, ThreadId:5 StopService Returns
15:04:18, ThreadId:5 OnStop Returns
15:04:18, ThreadId:2 Dispose Called
15:04:18, ThreadId:2 Dispose Returns
15:04:18, ThreadId:2 Main Returns
15:05:16, ThreadId:2 Main Called
15:05:16, ThreadId:5 OnStart Called, and Returns
15:05:50, ThreadId:2 OnShutdown Called
15:05:50, ThreadId:2 StopService Called
15:05:51, ThreadId:2 StopService Returns
15:05:51, ThreadId:2 OnShutdown Returns
public class C_TstSvc : ServiceBase
{
Thread oStartThread;
///--------------------------------------------------------------------
/// Constructor
///
/// Parameters:
/// void
///--------------------------------------------------------------------
public C_TstSvc()
{
ServiceName = "Test_Server";
this.CanStop = true;
this.CanShutdown = true;
}
///--------------------------------------------------------------------
/// The main entry point for the service
///
/// Parameters:
/// void
///--------------------------------------------------------------------
static void Main()
{
Log( "Main Called");
System.ServiceProcess.ServiceBase oServiceHanlder = new C_TstSvc();
System.ServiceProcess.ServiceBase.Run( oServiceHanlder);
Log( "Main Returns");
}
///--------------------------------------------------------------------
/// Startup the Tst as service
///
/// Parameters:
/// void
///--------------------------------------------------------------------
protected override void OnStart(string[] args)
{
Log( "OnStart Called, and Returns");
oStartThread = Thread.CurrentThread;
}
///--------------------------------------------------------------------
/// Stop the Tst service
///
/// Parameters:
/// void
///--------------------------------------------------------------------
protected override void OnStop()
{
Log( "OnStop Called");
StopService();
Log( "OnStop Returns");
}
///--------------------------------------------------------------------
/// Called when system shuts down
///
/// Parameters:
/// void
///--------------------------------------------------------------------
protected override void OnShutdown()
{
Log( "OnShutdown Called");
StopService();
Log( "OnShutdown Returns");
oStartThread.Abort();
}
///--------------------------------------------------------------------
/// Called when system shuts down
///
/// Parameters:
/// void
///--------------------------------------------------------------------
private void StopService()
{
Log( "StopService Called");
Thread.Sleep( 500);
Log( "StopService Returns");
}
static private void Log( string sMsg)
{
Thread iCurrentTread = System.Threading.Thread.CurrentThread;
StreamWriter m_oFileWriter;
m_oFileWriter = File.AppendText( "c:\\Aniware\\Test\\TestService.log");
m_oFileWriter.NewLine = "\r\n";
m_oFileWriter.WriteLine( DateTime.Now.ToLongTimeString() + ", ThreadId:"
+ iCurrentTread.GetHashCode() + " " + sMsg);
m_oFileWriter.Close();
}
///--------------------------------------------------------------------
/// Clean up resourses állocated by this object
///
/// Parameters:
/// void
///--------------------------------------------------------------------
protected override void Dispose(bool disposing)
{
Log( "Dispose Called");
base.Dispose (disposing);
Log( "Dispose Returns");
}
}