使用企業庫進行日誌和循環線程的windows服務的完整示例。刪除logger.write行,如果不使用企業庫
namespace Example.Name.Space
{
public partial class SmsServices : ServiceBase
{
private static String _state = "";
private ManualResetEvent _stop = new ManualResetEvent(false);
private static RegisteredWaitHandle _registeredWait;
public WindowsServices()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Logger.Write("Starting service", LoggerCategory.Information);
_stop.Reset();
_registeredWait = ThreadPool.RegisterWaitForSingleObject(_stop,
PeriodicProcess, null, 5000, false);
}
protected override void OnStop()
{
// UpdateTimer.Stop();
_stop.Set();
Logger.Write("Stopping service", LoggerCategory.Information);
}
private static void PeriodicProcess(object state, bool timedOut)
{
if (timedOut)
{
// Periodic processing here
Logger.Write("Asserting thread state", LoggerCategory.Debug);
lock (_state)
{
if (_state.Equals("RUNNING"))
{
Logger.Write("Thread already running", LoggerCategory.Debug);
return;
}
Logger.Write("Starting thread", LoggerCategory.Debug);
_state = "RUNNING";
}
Logger.Write("Processing all messages", LoggerCategory.Information);
//Do something
lock (_state)
{
Logger.Write("Stopping thread", LoggerCategory.Debug);
_state = "STOPPED";
}
}
else
// Stop any more events coming along
_registeredWait.Unregister(null);
}
}
}
我認爲一個很好的方法來加速解決這個問題是發佈計時器初始化和啓動代碼。 – Vokinneberg 2010-09-03 12:50:39
使用任何非託管代碼? – 2010-09-03 13:28:48
我打電話來自原生win32 dll函數,但它是在其他線程 – AFgone 2010-09-04 09:00:37