是否Application.Restart()创建的应用程序或没有新的进程?应用程序、进程、Application、Restart

2023-09-04 02:40:26 作者:我不爱你、别自讨没趣

据我所知 Application.Restart()重新启动应用程序并创建一个新的应用实例。请问这种情况下会产生新的过程中,还是老工艺将采用?

As I know Application.Restart() restarts an application and creates new Instance of an Application. Does this instance will creates in new process, or old process will be used?

感谢您的回答。

推荐答案

它运行在一个新的进程。该文档似乎就有点不清楚的过程是否被重复使用,但它可以通过示出在启动时在文本框的进程ID进行验证。

It runs in a new process. The documentation seems a little unclear on whether or not the process is reused but it can be verified by showing the process ID in a text box at start up.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Application.Restart();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = Process.GetCurrentProcess().Id.ToString();
    }
}

您还可以看到使用 .net反射在创建新进程:

You can also see using .NET Reflector that a new process is created:

public static void Restart()
{
    // ...
    ExitInternal();            // Causes the application to exit.
    Process.Start(startInfo);  // Starts a new process.
    // ...
}