Skip to main content
 首页 » 编程设计

C# :how to have message box within an event handler that freezes the application until Ok is pressed

2024年02月23日27yxwkf

你好,我主要有这个

  NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(AddressChangedCallback); 
//The main also contains a form 

和下面的这个方法。

static void AddressChangedCallback(object sender, EventArgs e){ 
// would like to have a message box here that freezes the entire application 
including  the form as mentioned above , untill OK is pressed" 
} 

请您参考如下方法:

我假设这是一个 WinForms 应用程序。 NetworkAddressChanged 事件在后台线程上调用。这就是为什么当您从那里显示消息框时您的应用程序保持事件状态。

解决方案 1: 您需要将此调用编码到您的主 UI 线程。您可以使用 Invoke 来做到这一点 主窗体上的方法。在窗体类上定义一个方法来显示消息框。在主窗体上使用 Invoke 方法调用此方法。

解决方案 2: C# / .NET messagebox is not modal