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

Yur*_*riy 6 .net c#

据我所知,Application.Restart()重新启动一个应用程序并创建一个新的应用程序实例.此实例是否会在新流程中创建,或者将使用旧流程?

谢谢你的回答.

Mar*_*ers 9

它运行在一个新的过程中.关于该过程是否被重用,文档似乎有点不清楚,但可以通过在启动时在文本框中显示进程ID来验证它.

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

您还可以使用.NET Reflector查看是否创建了新进程:

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