c#关闭窗体时终止其他进程
我想在窗体关闭时终止一些进程c#关闭窗体时终止其他进程,c#,kill-process,C#,Kill Process,我想在窗体关闭时终止一些进程 private void Form1_Closed(object sender, System.EventArgs e) { Process[] processList = Process.GetProcessesByName("notepad"); if(processList.Length > 0) { processList[0].K
private void Form1_Closed(object sender, System.EventArgs e)
{
Process[] processList = Process.GetProcessesByName("notepad");
if(processList.Length > 0)
{
processList[0].Kill();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Process[] processList = Process.GetProcessesByName("notepad");
if (processList.Length > 0)
{
processList[0].Kill();
}
}
像这样
但是这个来源不起作用
我想我需要做一些背景线索
如何关闭检查表单的后台线程?没有理由为您的操作创建后台线程。事实上,如果Form1是由
Application.Run(new Form1())
调用的主窗体,则当窗体关闭时,您创建的任何后台线程都将终止
在事件处理程序中放置断点以验证:
Form1.Closing+=新事件处理程序(Form1\u FormClosing);
Form1.Closed+=新事件处理程序(Form1\u FormClosed)代码>
process.GetProcessesByName(“记事本”)返回。
如果您在尝试上述操作后仍有问题,请更新您的帖子。确保您的活动已正确连接。 这里还有一些额外的输入。您正在杀死列表中找到的第一个记事本实例 如果该实例属于另一个用户,会发生什么情况 您应该检查进程的所有者,以确保它属于当前用户 如果用户打开了多个记事本实例,会发生什么情况
//get our seesion id for current user running the app
int currentUser = Process.GetCurrentProcess().SessionId;
//get a list where process equals notepad and session id is current user
List<Process> currentProcesses = Process.GetProcessesByName("notepad").Where(p => p.SessionId.Equals(currentUser)).ToList();
//used list for .ForEach less code to write
currentProcesses.ForEach(p => p.Kill());
//获取当前运行应用程序的用户的seision id
int currentUser=Process.GetCurrentProcess().SessionId;
//获取一个列表,其中进程等于记事本,会话id为当前用户
List currentprocess=Process.GetProcessesByName(“记事本”)。其中(p=>p.SessionId.Equals(currentUser)).ToList();
//用于编写.ForEach更少代码的列表
currentProcesses.ForEach(p=>p.Kill());
我解决了这个问题
问题在于扼杀进步
Process[] localByName = Process.GetProcessesByName("notepad");
foreach (Process p in localByName)
{
p.Kill();
}
它怎么不起作用?您的代码是否正在执行,但却找不到任何东西?可能你需要寻找
notepad.exe
。你需要背景线程做什么?什么是不工作?事件中的代码会工作。我测试过了。我猜你的事件没有触发。我对c#知之甚少,我想在表单关闭时杀死notepad.exe,所以我添加了以上两个函数,但它没有work@AndrewArnold它应该是没有延伸的,他所做的事情应该按原样进行。