Search code examples
c#.netshowdialog

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


Hi I have this in the main

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

and this method below it .

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"
}

Solution

  • I'm assuming this is a WinForms application. NetworkAddressChanged event is invoked on a background thread. This is why when you display a message box from there your app stays active.

    Solution 1: You need to marshal this call to your main UI thread. You can do this by using Invoke method on your main form. Define a method on the form class to show message box. Call this method using Invoke method on your main form.

    Solution 2: C# / .NET messagebox is not modal