![](/img/trans.png)
[英]Error 1053 the service did not respond to the start or control request in a timely fashion
[英]C# Error 1053 the service did not respond to the start or control request in a timely fashion
我意識到有很多這樣的帖子,但不管您信不信,都可以解決我的問題。
我這里有以下代碼,如果運行時間太長,則使用ManagementEventWatcher類從另一個內部應用程序中終止進程,該進程有時會執行並終止cpu。
無論如何,啟動服務時它會立即收到此錯誤。 事件日志中沒有任何內容。 目前,我已經使用notepad.exe對其進行了測試。
public AppXKiller()
{
InitializeComponent();
this.ServiceName = "AppXKiller";
this.EventLog.Log = "Application";
// These Flags set whether or not to handle that specific
// type of event. Set to true if you need it, false otherwise.
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
}
static void Main()
{
ServiceBase.Run(new AppXKiller());
}
protected override void OnStart(string[] args)
{
registerWatcher();
}
protected override void OnContinue()
{
base.OnContinue();
}
public void registerWatcher()
{
string pol = "2";
string appName = "notepad.exe";
string queryString =
"SELECT *" +
" FROM __InstanceOperationEvent " +
"WITHIN " + pol +
" WHERE TargetInstance ISA 'Win32_Process' " +
" AND TargetInstance.Name = '" + appName + "'";
// You could replace the dot by a machine name to watch to that machine
string scope = @"\\.\root\CIMV2";
// create the watcher and start to listen
ManagementEventWatcher watcher = new ManagementEventWatcher(scope, queryString);
watcher.EventArrived += new EventArrivedEventHandler(this.OnEventArrived);
watcher.Start();
}
private void OnEventArrived(object sender, EventArrivedEventArgs e)
{
Thread.Sleep(20000);
Process[] localByName = Process.GetProcessesByName("notepad");
if (localByName.Length > 0)
{
localByName[0].Kill();
}
}
protected override void OnStop()
{
}
}
原來,該應用程序必須是內部版本,而不是調試版本。 這沒有任何意義,但是很好。 我想我是否要測試和調試應用程序,必須在發布模式下進行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.