[英]C#: Windows Console Application to Windows Service
我有一個控制台應用程序,它可以跟蹤計算機的性能,並且正在嘗試使其成為Windows服務。 該服務安裝得很好,但是當我進入任務管理器並嘗試運行該服務時,出現錯誤1053:該服務未及時響應啟動或控制請求。 在任務管理器中,服務有時會處於“正在啟動”狀態,但不會運行。 最重要的是,當我運行控制台應用程序時,我還收到一條錯誤消息,指出: 無法從命令行或調試器啟動服務。 必須先安裝Windows服務 -我已經完成了。 這是我的程序類:
public static string ServiceName = "performanceService";
static void Main(string[] args)
{
if (!Environment.UserInteractive)
{
PerformanceCounter ramCount = new PerformanceCounter("Memory", "Available MBytes");
PerformanceCounter cpuCount = new PerformanceCounter("Processor", "% Processor Time", "_Total");
Console.WriteLine("Press any key to view other information...\n");
Console.WriteLine("CPU and RAM information");
while (!Console.KeyAvailable)
{
double perf = cpuCount.NextValue();
Console.WriteLine("CPU Performance: " + perf + " %");
代碼繼續計算性能並將其顯示在控制台中。
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceControl()
};
ServiceBase.Run(ServicesToRun);
}
}
這是我的ServiceControl類:
public ServiceControl()
{
ServiceName = Program.ServiceName;
InitializeComponent();
}
public void Start()
{
OnStart(null);
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
}
private void InitializeComponent()
{
//
// ServiceControl
//
this.ServiceName = "performanceService";
}
為了安裝該服務,老實說,出於好奇,我同時使用了Project Installer和手動安裝。
我通過regedit更改了時間限制,但這沒有幫助。
如果您需要查看其他代碼,請告訴我。
我的問題是,我是否在代碼中缺少某些內容,從而無法啟動我的服務? 謝謝你的幫助。
謝謝你的幫助。 問題是我在ServiceControl類中具有Start()方法,而在Program類中卻具有其他代碼。
我應該在if語句中具有啟動服務的代碼,在else中具有性能邏輯,以便啟動方法可以快速執行,就像建議的注釋之一一樣。 在任務管理器中單擊“啟動”后,狀態變為“正在運行”。
再次感謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.