我意识到这些帖子有很多,但是没有,不管你信不信,解决了我的问题。
我在这里使用以下代码,如果它运行时间过长,则使用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()
{
}
}
答案 0 :(得分:2)
原来应用程序必须是构建版本,而不是调试版本。这没有任何意义,但很好。我想如果我想测试和调试应用程序,我必须在发布模式下进行。