Jeffrey&Lynny

一个温馨小家庭的.Net生活

导航

.Net Windows Service


static void Main()
{
    System.ServiceProcess.ServiceBase[] ServicesToRun;

    // More than one user Service may run within the same process. To add
    // another service to this process, change the following line to
    // create a second service object. For example,
    //
    //   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
    //
    ServicesToRun = new System.ServiceProcess.ServiceBase[] new Service1() };

    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}


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:

this.startCompletedSignal = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(
new WaitCallback(this.ServiceQueuedMainCallback), textArray1);
this.startCompletedSignal.WaitOne();

}

private void ServiceQueuedMainCallback(object state)
{
      
string[] textArray1 = (string[]) state;
      try
      
{
            this.OnStart(textArray1);
            
this.WriteEventLogEntry(Res.GetString("StartSuccessful"));
            
this.status.currentState = 4;
      }

      
catch (Exception exception1)
      
{
            
this
.WriteEventLogEntry(Res.GetString("StartFailed", new object[] { exception1.ToString() }), EventLogEntryType.Error);
            
this.status.currentState = 1;
      }

      
this.startCompletedSignal.Set();
}


 

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.
}

private unsafe void DeferredStop()
{
      
fixed (NativeMethods.SERVICE_STATUS* service_statusRef1 = &this.status)
      
{
            
int num1 = this.status.currentState;
            
this.status.checkPoint = 0;
            
this.status.waitHint = 0;
            
this.status.currentState = 3;
            NativeMethods.SetServiceStatus(
this.statusHandle, service_statusRef1);
            try
            
{
                  this.OnStop();
                  
this.WriteEventLogEntry(Res.GetString("StopSuccessful"));
                  
this.status.currentState = 1;
                  NativeMethods.SetServiceStatus(
this.statusHandle, service_statusRef1);
            }

            
catch (StackOverflowException)
            
{
                  
throw;
            }

            
catch (OutOfMemoryException)
            
{
                  
throw;
            }

            
catch (ThreadAbortException)
            
{
                  
throw;
            }

            
catch (Exception exception1)
            
{
                  
this
.WriteEventLogEntry(Res.GetString("StopFailed", new object[] { exception1.ToString() }), EventLogEntryType.Error);
                  
this.status.currentState = num1;
            }

      }

}


 

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.


 

posted on 2006-08-07 17:52  比尔盖房  阅读(642)  评论(0编辑  收藏  举报