C# 运行Windows窗体应用程序的多个实例
我决定创建一个聊天应用程序,我需要一个表单运行2次,也就是说,我需要在运行同一个表单时将其视为两个不同的实例。。。有可能实现吗C# 运行Windows窗体应用程序的多个实例,c#,.net,winforms,multiple-instances,C#,.net,Winforms,Multiple Instances,我决定创建一个聊天应用程序,我需要一个表单运行2次,也就是说,我需要在运行同一个表单时将其视为两个不同的实例。。。有可能实现吗 注意:我不需要在任何活动中都这样做。无论何时运行程序,都必须运行2个实例。是的,您可以轻松创建同一表单的多个实例。例如: new ChatWindowForm.Show(); 另请参阅方法文档。是的,您可以轻松创建同一表单的多个实例。例如: new ChatWindowForm.Show(); 另请参阅方法文档。两种解决方案: 只需构建可执行文件并根据需要运行尽可能
注意:我不需要在任何活动中都这样做。无论何时运行程序,都必须运行2个实例。是的,您可以轻松创建同一表单的多个实例。例如:
new ChatWindowForm.Show();
另请参阅方法文档。是的,您可以轻松创建同一表单的多个实例。例如:
new ChatWindowForm.Show();
另请参阅方法文档。两种解决方案:
只需构建可执行文件并根据需要运行尽可能多的实例。
使用更复杂的解决方案,利用。请参见下面的简化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);
}
}
两种解决方案:
只需构建可执行文件并根据需要运行尽可能多的实例。
使用更复杂的解决方案,利用。请参见下面的简化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);
}
}
如果你能展示你代码中的相关部分,我们可以提供更有意义的答案。我刚开始,一直在解决这个问题。。。你想要哪种代码…?如果你能展示代码的相关部分,我们可以提供更有意义的答案。。。你期待哪种代码…?我应该调用这个函数,在constructor中还是在何处?你会在任何情况下调用这个函数来启动一个新表单-在这种情况下,我猜它将是一个聊天窗口。如果这是按钮单击的一部分,则可以将其放入单击处理程序中。请编辑您的问题以包含您当前的一些代码。war我应该在constructor或where中调用此函数吗?您会在负责启动新表单的任何事件中调用此函数-在这种情况下,我猜它将是一个聊天窗口。如果这是按钮单击的一部分,则可以将其放入单击处理程序中。请编辑您的问题以包含一些当前代码。我认为这实际上是问题想要的答案。在按钮事件中,当用户按下按钮时,将打开一个新表单或显示一个重新打开的表单时,我如何在另一个类中使用ApplicationContext?我认为这实际上是问题想要的答案。我如何使用ApplicationContext在另一个类中的按钮事件中,当用户按下按钮时,将打开一个新表单或显示一个重新打开的表单??