我如何重复检查进程是否正在运行并杀死它,如果它是C#.NET(Windows窗体应用程序) [英] How do I repetitively check if a process is running in and kill it if it is C# .NET (windows form app)
本文介绍了我如何重复检查进程是否正在运行并杀死它,如果它是C#.NET(Windows窗体应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我想重复检查一个进程是否正在运行,如果是,我想杀死它。到目前为止,我可以让它在运行时执行一次,但用户可以在运行我的目标程序后运行目标程序(我要杀死的程序)。
我尝试了什么:
我试过
So, I want to repetitively check if a process is running and if it is I want to kill it. So far I can make it do it once at run-time but users can run the target program (the one I want to kill) after they have run my one.
What I have tried:
I have tried
private void Form1_Load(object sender, EventArgs e)
{
foreach (var process in Process.GetProcessesByName("TARGETPROGRAMNAME"))
{
process.Kill();
}
但这只发生在程序运行一次,意味着它们可以运行目标程序在我的那个之后。
But this only happens once upon the running of the program meaning they can run the target program after my one.
推荐答案
最简单的方法是设置一个System.Timers.Timer对象,每隔n秒打勾一次,并将你的代码放在tick事件处理程序中。
或者,您可以使用TPL。
或者,您可以使用System.Reactive
。
或者,你可以使用普通线程。
The simplest way is to setup a System.Timers.Timer object to tick every n seconds, and put your code in the tick event handler.
Or, you could use TPL.
Or, you could useSystem.Reactive
.
Or, you could use normal threads.
Lemmie guess,y '都想杀死任务经理。
Lemmie guess, y'all wanna kill Task Manager.
这篇关于我如何重复检查进程是否正在运行并杀死它,如果它是C#.NET(Windows窗体应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文