我試圖運行一些控制檯應用程序,Windows服務,我也跟着this question,我做了一些修改,以使其適合於我的應用程序。C#運行控制檯應用程序作爲Windows服務 - 該服務沒有響應錯誤
我主要的代碼看起來像這樣:
public static class Program
{
public class Service : ServiceBase
{
public Service(string serviceName)
{
this.ServiceName = serviceName;
}
protected override void OnStart(string[] args)
{
Program.Start(args);
}
protected override void OnStop()
{
Program.Stop(this.ServiceName);
}
}
#endregion
static void Main(string[] args)
{
if (!Environment.UserInteractive)
// running as service
using (var service = new Service("TestService"))
ServiceBase.Run(service);
else
{
// running as console app
Start(args);
}
}
private static void Start(string[] args)
{
while(true)
{
//DO SOMTHING
}
}
private static void Stop(string serviceName)
{
//Writing to log that 'serviceName' stopped
}
}
我試圖運行下面的控制檯應用程序作爲一種服務,通過使用以下步驟:
1)使用命令:sc創建ServiceTestName123 binPath =「路徑到項目調試文件夾中的EXE文件」。
2)使用命令:sc啓動ServiceTestName123 「參數1」。
而且我得到了一個錯誤: 「StartService失敗1053: 服務沒有及時到開始或控制請求」
我瞭解在互聯網上的錯誤,並發現我可以嘗試通過運行另一個線程啓動功能來解決這個問題,所以我更新了的OnStart功能下列功能:
protected override void OnStart(string[] args)
{
Thread t = new Thread(() => Program.Start(args));
t.Start();
}
試圖重新創建服務後(刪除舊的和創建服務再次用新的OnStart函數)並重新運行它,我得到了相同的錯誤要麼。
順便說一句,當我跑這個代碼作爲控制檯應用程序,一切正常。
可能有人請explaing我什麼我做錯了?
非常感謝。
這是一個一般錯誤消息,你應該檢查確切的錯誤信息Windows事件查看器。 – Winnie