単一のフォームを 2 回実行する必要がある (つまり、実行中に同じフォームを 2 つの異なるインスタンスとして表示する必要がある) チャット アプリケーションを作成することにしました。
注:どのイベントでも行う必要はありません。プログラムを実行するたびに、2 つのインスタンスを実行する必要があります。
単一のフォームを 2 回実行する必要がある (つまり、実行中に同じフォームを 2 つの異なるインスタンスとして表示する必要がある) チャット アプリケーションを作成することにしました。
注:どのイベントでも行う必要はありません。プログラムを実行するたびに、2 つのインスタンスを実行する必要があります。
2 つのソリューション:
Application.Run メソッド (ApplicationContext)を利用するより複雑なソリューションを使用します。以下の簡略化された MSDN の例を参照してください。
class MyApplicationContext : ApplicationContext
{
private int formCount;
private Form1 form1;
private Form1 form2;
private MyApplicationContext()
{
formCount = 0;
// Create both application forms and handle the Closed event
// to know when both forms are closed.
form1 = new Form1();
form1.Closed += new EventHandler(OnFormClosed);
formCount++;
form2 = new Form1();
form2.Closed += new EventHandler(OnFormClosed);
formCount++;
// Show both forms.
form1.Show();
form2.Show();
}
private void OnFormClosed(object sender, EventArgs e)
{
// When a form is closed, decrement the count of open forms.
// When the count gets to 0, exit the app by calling
// ExitThread().
formCount--;
if (formCount == 0)
ExitThread();
}
[STAThread]
static void Main(string[] args)
{
// Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit.
MyApplicationContext context = new MyApplicationContext();
// Run the application with the specific context. It will exit when
// all forms are closed.
Application.Run(context);
}
}