Hi everyone,

I have a windows app and a method hooked up to the Application.Idle event. When Itry to display a dialog (calling Form.ShowDialog), thedialog is displayed for a split second, then the Application.Idle event gets triggered andthe same thread that isshowing the dialog gets kidnapped toexecute my OnApplicationIdleMethod.If the method throws an unhandled exception, my entire application goes down without the dialog ever becoming visible to the user. If I use a MessageBox.Show instead of the dialog, the behavior is different: after the message box is displayed and WHILE it is displayed, the thread never goes off executingthe OnApplicationIdle method, which allows the user to click OK on the message box, and only THEN for the application to crash. So, the thread is blocked waiting for user input while the message box is displayed, but the thread is not blocked and enters OnApplicationIdle while the dialog is displayed.

My question is, how can I block the UI thread from doing any background event processing while my dialog is displayed? It is critical for the dialog to be shown to the user and not be vulnerable to what happens in OnApplicationIdle, or any other event handler that can throw an exception. I am also trying to understand why the behavior of Form.ShowDialog and MessageBox.Show is different.

Here is the sample code - if you uncomment MessageBox.Show, you will be able to see a message box and click OK before the application crashes, but you will never see the dialog displayed (The ErrorDialog class is just a simple form with an OK button on it):

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

Application.Idle += new EventHandler(Application_Idle);

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

}

void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)

{

//MessageBox.Show("TEST");

ErrorDialog errdlg = new ErrorDialog();

errdlg.ShowDialog();

}

void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

{

//MessageBox.Show("TEST1");

ErrorDialog errdlg = new ErrorDialog();

errdlg.ShowDialog();

}

void Application_Idle(object sender, EventArgs e)

{

string a = String.Empty;

for (int i = 0; i < 10000; i++)

{

a = i.ToString();

if (i == 800)

{

i = Int32.Parse("afsdf");

}

}

}

}

}

Any help will be greatly appreciated.