I have a .NET based Excel addin that displays a custom toolbar in Excel. There are about 30 commands that can be executed through this toolbar. The commands are executed on the same (Excel) thread.
I want to display progress dialog for some of the commands that take some time to execute for which I used a non-modal dialog. The dialog works fine but because its running on the same thread, the dialog UI (Cancel button) is not responsive during a long operation. I have to click the Cancel button a number of times before the click is actually captured by the dialog.
One of the solution that I saw on SO is to use the BackgroundWorker for all tasks but I want the operations to work on the same thread as Excel (do not want to change the existing working code). I want the progress dialog to go on a separate thread so I created a Form class with a ShowProgressDialog(IWin32Window wnd) method like this:
public void ShowProgressDialog(IWin32Window wnd)
{
// show dialog in a different thread
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(delegate(object state)
{
this.ShowDialog(wnd);
}));
}
The Cancel button on the form displays a MessageBox ('Are you sure you want to cancel' type) and uses a ManualResetEvent to make the calling thread wait until the MessageBox is dismissed. Though it works fine, the problem comes if I keep the MessageBox waiting for more than a minute. Excel flashes in taskbar and stops responding (AppHang) and finally crashes.
Do anyone of you have a better solution to display responsive progress dialogs? OR any idea why Excel crashes after waiting for about a minute (maybe because of the WaitOne() call)?