.Net Windows Servicestatic void Main()
}


ServiceBase.Run()
{
1. Initialize each ServiceEntry structure(setting the all the callback routines)
2. Invoke StartServiceCtrlDispatcher();
3. Write eventlog if StartServiceCtrlDispatcher failed.
}

ServiceBase.ServiceMainCallback()
{
1. Call RegisterServiceCtrlHandlerEx
2. Set SERVICE_START_PENDING
3. Start a new thread and wait it to invoke OnStart:

.Net Windows Servicethis.startCompletedSignal = new ManualResetEvent(false);
.Net Windows ServiceThreadPool.QueueUserWorkItem(
new WaitCallback(this.ServiceQueuedMainCallback), textArray1);
.Net Windows Service
this.startCompletedSignal.WaitOne();

}

.Net Windows Serviceprivate void ServiceQueuedMainCallback(object state)

Note:
1. This worker thread method catches all exceptions in OnStart method.
2. The currentState is set to SERVICE_RUNNING(4)
3. Start success or failed is logged automatically.

unsafe ServiceBase.ServiceCommandCallback() //HandlerEx func
{
1. For Stop/Pause operations, the general priciple is setting the status to PENDING, then use DeferredHandlerDelegate to invoke DeferredStop(or DeferredPause)
2. OnContinue, OnShutdown and OnCustomCommand are all called directly. All exceptions in these methods are caught and all operations success/failure are logged to eventlog.
}

.Net Windows Serviceprivate unsafe void DeferredStop()

Note:
1. STOP_PENDING is set with checkPoint and waitHint both set to 0.(This looks strange!)
2. OnStop is called and then STATUS_STOPPED is set.
3. All exception is caught, except StackOverflowException, OutOfMemoryException, ThreadAbortException.
4. Stop operation success/failure is automatically logged.

Misc info:
1. OnStart is invoked in a background threadpool thread by ThreadPool.QueueUserWorkItem While OnStop/OnPause are invoked with delegate BeginInvoke, which runs in another background thread. So they are all run in different threads, not the main thread.
2. OnContinue, OnShutdown and OnCustomCommand are all called directly by *HandlerEx*, so they are all in the main thread.


 

相关文章:

  • 2022-01-28
  • 2021-12-01
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-07-03
  • 2021-08-22
  • 2021-12-29
  • 2021-12-26
  • 2021-10-04
  • 2021-12-07
  • 2022-12-23
相关资源
相似解决方案