问题
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