[英]C# Error 1053 the service did not respond to the start or control request in a timely fashion
I realize there are a ton of these posts, but none them, believe it or not, resolve my problem. 我意识到有很多这样的帖子,但不管您信不信,都可以解决我的问题。
I have the following code here that uses the ManagementEventWatcher class to kill a process from another in house app if it runs too long, which it occasionally does and kills the cpu. 我这里有以下代码,如果运行时间太长,则使用ManagementEventWatcher类从另一个内部应用程序中终止进程,该进程有时会执行并终止cpu。
Anyway, it gets this error instantaneously when starting the service. 无论如何,启动服务时它会立即收到此错误。 Nothing in the event log. 事件日志中没有任何内容。 Currently I have it testing with notepad.exe. 目前,我已经使用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()
{
}
}
Turns out that the application has to be the release version of the build, not the debug version. 原来,该应用程序必须是内部版本,而不是调试版本。 This makes no sense but oh well. 这没有任何意义,但是很好。 I guess if I want to test and debug the app I have to do it in release mode. 我想我是否要测试和调试应用程序,必须在发布模式下进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.