简体   繁体   English

C#/ .NET:关闭主窗口外的另一个进程

[英]C#/.NET: Closing another process outside the main window

I just wanna ask your opinion/suggestion on how to 'terminate' a running application/process is C# 我只是想问一下你如何'终止'正在运行的应用程序/进程的意见/建议是C#

Right now, I do have the following codes: 现在,我有以下代码:

    Process myProcess;
    private void btnOpen_Click(object sender, RoutedEventArgs e)
    {
        DirectoryInfo di = new DirectoryInfo(System.Environment.GetFolderPath(Environment.SpecialFolder.Programs));
        myProcess = Process.Start(di + @"\Wosk\Wosk.appref-ms"); // opening a file coming for Startup Menu
    }

    private void btnClose_Click(object sender, RoutedEventArgs e)
    {
        myProcess.Kill(); // not working - Cannot process request because the process has exited
    }

I also tried myProcess.Close(); 我也试过myProcess.Close(); but nothing's happening. 但什么都没发生。

Process[] islemler = Process.GetProcessesByName("osk");
foreach (Process islem in islemler)
    islem.Kill();

You should have a look at 你应该看看

Process.HasExited Property Process.HasExited Property

A process can terminate independently of your code. 进程可以独立于您的代码终止。 If you started the process using this component, the system updates the value of HasExited automatically, even if the associated process exits independently. 如果使用此组件启动了该过程,则系统会自动更新HasExited的值,即使关联的进程独立退出也是如此。

Based on your comment it looks like the Process instance has already exited when you hit the close button. 根据您的注释,当您点击关闭按钮时,看起来Process实例已经退出。 This can happen at any time and it's something you need to guard against. 这可以在任何时候发生,这是你需要防范的事情。 The easiest way is to simply catch the exception that results from calling Kill on an already exited process. 最简单的方法是简单地捕获在已经退出的进程上调用Kill导致的异常。

try {
  myProcess.Kill();
} catch ( InvalidOperationException ) {
  // Process is already finished so nothing to do
}

You are starting a program that was installed with ClickOnce. 您正在启动与ClickOnce一起安装的程序。 The .appref-ms is executed by a helper program, rundll32.exe, that starts the process and quickly exits. .appref-ms由辅助程序rundll32.exe执行,该程序启动进程并快速退出。 To terminate the started process, you'll need to find the actual running .exe with Process.GetProcessesByName() and use the Kill method. 要终止已启动的进程,您需要使用Process.GetProcessesByName()找到实际运行的.exe并使用Kill方法。

We can't tell you what the process name is, that's contained in the .appref-ms file. 我们无法告诉您进程名称是什么,包含在.appref-ms文件中。 But it is easy for you to see with TaskMgr.exe. 但是使用TaskMgr.exe很容易看到。

First please replace: 首先请更换:

di + @"\Wosk\Wosk.appref-ms" 

with: 有:

Path.Combine(di.FullName, @"Wosk\Wosk.appref-ms")

Now to the point: I don't know what Wosk.appref-ms is or how this process is started. 现在到了这Wosk.appref-ms :我不知道Wosk.appref-ms是什么或者这个过程是如何开始的。 If this is a file it will be opened with the default program associated with this file extension. 如果这是一个文件,它将使用与此文件扩展名关联的默认程序打开。 The problem could be related to the fact that the process you start only starts another process and terminates immediately. 问题可能与您启动的进程仅启动另一个进程并立即终止有关。 That's why when you try to kill it it says that it has already exited, but the actual process it spawned is still running. 这就是为什么当你试图杀死它时它说它已经退出了,但它产生的实际过程仍在运行。 In this case you will have to enumerate through the running processes with Process.GetProcesses() , find the process and stop it. 在这种情况下,您将必须使用Process.GetProcesses()枚举正在运行的进程,找到该进程并将其停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM