private void CheckCommandQueue() { if (CommandQueue.Count != 0) { string command = CommandQueue.Dequeue() as string; if (command == ServiceCommand.STOP) { Status = ServiceStatus.ZOMBIE; Logger.Info("Service is going to be stopped"); } else if (command == ServiceCommand.SUSPEND) { Status = ServiceStatus.PENDING; Logger.Info("Service is going to be paused"); } else if (command == ServiceCommand.RESUME) { Status = ServiceStatus.RUNNING; Logger.Info("Service is going to be resumed"); } else { Logger.Error("Error Service Command Type"); //Debug.Assert(false); } } }
public void MainLoop(HandleSMSMessage hanleSMSException) { SMSHandle = hanleSMSException; // init stage Logger = new QCLogger(Name); Options = InitOptions(); CommandQueue = Queue.Synchronized(new Queue()); Logger.Info("QCService Init Start"); try { Init(); } catch (Exception ie) { Logger.LogException(ie); Logger.Error("QCService Init Failed. Service is shutting down error:" + ie.Message); throw (ie); } Status = ServiceStatus.RUNNING; // init vars int idleInterval = Options.IdleInteval; //Options.GetInt("IdleInterval"); int lastIdleTime = DateTime.Now.Millisecond; int checkInterval = 10000; Logger.Info("QCService Init End "); Logger.Info("QCService Start Main Loop"); Int64 loopTimes = 0; // wokring loop try { int i = 0; while (true) { CheckCommandQueue(); if (Status == ServiceStatus.PENDING) { if (loopTimes++ % checkInterval == 0) { Logger.Info("Service is Paused. Everything is fine :-)"); } Thread.Sleep(idleInterval); } else if (Status == ServiceStatus.ZOMBIE) { Logger.Info("Service is stopping. Good bye. :-)"); break; } else if (Status == ServiceStatus.RUNNING) { QCServiceWorkState result = QCServiceWorkState.Idle; try { result = Work(); } catch (Exception e) { Logger.LogException(e); } if (result == QCServiceWorkState.Idle) { int now = DateTime.Now.Millisecond; if (now - lastIdleTime < idleInterval) { Thread.Sleep(idleInterval); } lastIdleTime = now; if (loopTimes++ % checkInterval == 0) { Logger.Info("Service is Idle. Everything is fine :-)"); } } } else { Debug.Assert(false); } } } catch (QCServiceTerminalException e) { Logger.Debug(e.Message); } // end stage Logger.Info("QCService Finishing"); Finish(); Logger.Info("QCService Finished"); }