启动和在C#.NET停止进程进程、NET

2023-09-05 22:59:13 作者:︶︿︶不想长大

我想towrite一个简单的程序,有两种方法,一种是将启动需要上下一致的过程一个过程和一个。 如:

I am trying towrite a simple program that has two methods, one that starts a process and one that takes down the same process. as in:

public Process StartProc(string procname)
{
    Process proc = new Process();
    proc.StartInfo.FileName = procname;
    proc.Start();
    return proc;
}

public void StopProc(Process proc)
{
    proc.Close();
}

是否有可能做这样?

Is it possible to do this like that?

推荐答案

是的,你是后该方法被称为Kill,没有关闭

Yes, the method you are after is called Kill, not Close:

public void StopProc(Process proc)
{
    proc.Kill();
}

这将强行关闭进程 - whever可能是preferable信号的应用程序关闭,如通过请求应用程序关闭主窗口:

This will forcibly close the process - whever possible it is preferable to signal the application to close such as by requesting that the application close the main window:

public void StopProc(Process proc)
{
    proc.CloseMainWindow();
}

这允许应用程序执行清理逻辑(例如保存文件),但是可以允许进程继续运行,如果它选择忽略请求,并会做什么,如果这个过程没有一个主窗口(例如使用一个控制台应用程序)。

This allows the application to perform clean-up logic (such as saving files), however may allow the process to continue running if it chooses to ignore the request and will do nothing if the process does not have a main window (for example with a console application).

有关详细信息,请参见Process.CloseMainWindow方法。

For more information see the documentation on the Process.CloseMainWindow method.