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

发布于 2024-12-03 08:10:00 字数 303 浏览 0 评论 0原文

我正在尝试编写一个简单的程序,它有两种方法,一种启动进程,一种停止同一进程。 如:

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

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

可以这样做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹎☆浅夏丿初晴 2024-12-10 08:10:00

是的,您使用的方法称为 Kill,而不是Close

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

这将强制关闭进程 - 如果可能,最好向应用程序发出关闭信号,例如请求应用程序关闭主窗口:

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

这允许应用程序执行清理逻辑(例如保存文件),但是如果它选择忽略请求,则可能允许进程继续运行,并且如果进程没有主窗口(例如使用控制台应用程序),则不会执行任何操作。

有关详细信息,请参阅有关 进程的文档。 CloseMainWindow 方法。

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

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

This will forcibly close the process - when 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).

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

伤感在游骋 2024-12-10 08:10:00

我认为您正在寻找 Process.Kill( )

您实际上并不需要 StopProc() 方法,您可以直接编写 proc.Kill()

但是,通常不建议您以如此残酷的方式终止进程。这样做可能会使共享对象处于未定义状态。如果您能找到一种方法来合作关闭该流程,那就是首选。

I think you are looking for Process.Kill().

You don't really need a StopProc() method, you can just write proc.Kill() directly.

However, it is not generally recommended that you terminate processes in such a brutal way. Doing so can leave shared objects in an undefined state. If you can find a way to co-operatively close the process that is to be preferred.

空宴 2024-12-10 08:10:00

通过启动进程,您可以获得该进程的唯一 ID,然后您可以像这样杀死它:

public static int Start(string processName)
    {
        var process =
            Process.Start(processName);
        return
            process.Id;
    }

    public static void Stop(int processId)
    {
        var process =
            Process.GetProcessById(processId);
        process.Kill();
    }

By starting the process, you can get the unique Id of that process and then you can kill it like this:

public static int Start(string processName)
    {
        var process =
            Process.Start(processName);
        return
            process.Id;
    }

    public static void Stop(int processId)
    {
        var process =
            Process.GetProcessById(processId);
        process.Kill();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文