How do I create another instance of a .NET program within one instance by code?

岁酱吖の 提交于 2020-01-24 05:22:07

问题


I need to be able to create another brand new instance of a program on a button click while keeping the existing instance.

this.ShowDialog(new Form1());

The above statement causes the current form to be the owner of the new form and I need the second instance to be independent of the existing instance.

Can anyone help me with this?


回答1:


To expound on Desolator's answer here is a simplistic example you can try a Form and a Button:

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

    private void button1_Click(object sender, EventArgs e)
    {
        Process p = new Process();
        p.StartInfo.FileName = Application.ExecutablePath;
        p.Start();
    }
}



回答2:


You can use instead new Form1().Show();, but when your current instances exists, the other one will exit too. So, to be fully independent, it is better to use System.Diagnostics.Process.Start(string path) which starts your program exactly as if one double clicks it.




回答3:


(new Form1()).Show();

(new Form1()).Show();


来源:https://stackoverflow.com/questions/11332731/how-do-i-create-another-instance-of-a-net-program-within-one-instance-by-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!