我已經構建了一個Windows服務來監視我們的服務器上的一些設置,我已經開發了不少WinForm和WPF應用程序,但對於Windows服務我是一個絕對的新手,這就是爲什麼我訴諸於msdn,並按照教程how to create a simple service。現在,我可以安裝該服務,並使其運行,但只有當我從微軟教程中刪減了一些零碎......但我很好奇爲什麼,當我按照教程,我的服務在啓動時得到一個意外的錯誤。Windows服務 - 在啓動時崩潰
經過一些測試,似乎該服務似乎OnStart方法崩潰在SetServiceStatus()
public partial class MyService: ServiceBase
{
private static ManualResetEvent pause = new ManualResetEvent(false);
[DllImport("ADVAPI32.DLL", EntryPoint = "SetServiceStatus")]
public static extern bool SetServiceStatus(IntPtr hServiceStatus, SERVICE_STATUS lpServiceStatus);
private SERVICE_STATUS myServiceStatus;
private Thread workerThread = null;
public MyService()
{
InitializeComponent();
CanPauseAndContinue = true;
CanHandleSessionChangeEvent = true;
ServiceName = "MyService";
}
static void Main()
{
// Load the service into memory.
System.ServiceProcess.ServiceBase.Run(MyService());
}
protected override void OnStart(string[] args)
{
IntPtr handle = this.ServiceHandle;
myServiceStatus.currentState = (int)State.SERVICE_START_PENDING;
**SetServiceStatus(handle, myServiceStatus);**
// Start a separate thread that does the actual work.
if ((workerThread == null) || ((workerThread.ThreadState & (System.Threading.ThreadState.Unstarted | System.Threading.ThreadState.Stopped)) != 0))
{
workerThread = new Thread(new ThreadStart(ServiceWorkerMethod));
workerThread.Start();
}
myServiceStatus.currentState = (int)State.SERVICE_RUNNING;
SetServiceStatus(handle, myServiceStatus);
}
}
現在我的服務似乎運行得很好,當我註釋掉SetServiceStatus()
線。爲什麼這會失敗?這是一個權利問題還是我完全忽略了這一點?
你有調用'SetServiceStatus'的一些具體原因嗎?這不是嚴格要求,你可能只是擺脫這些電話。 – CodingGorilla 2013-02-27 17:55:34
你可以按照這個簡單的教程來做一個簡單的Windows服務。所有這些外部電話都不需要您的服務工作。 [http://www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C] – 2013-02-27 19:12:15
那麼我沒有具體的原因,只是試圖跟隨msdn,並想知道爲什麼在地球上它失敗了。 – XikiryoX 2013-02-27 22:05:30