Background:
I am using a simple progress dialog in an Outlook Add-in to show progress while performing long operations. Since I cannot run code that uses Outlook objects in a separate thread, I cannot implement a more traditional background worker process. My add-in has been working OK until Outlook 2013 where in certain instances my progress dialog hangs. When I run the add-in in the VS debugger and cause the hang, then do a break, it appears to be stuck on the DoEvents() line that tries to force the progressbar to update.
My Question:
Can someone suggest a better system to show progress with the restriction above (long running code must run in main Outlook thread). Is there a better way to make the progress dialog responsive without using DoEvents()?
The following simple code demonstrates how I am doing this now. In the add-in code that is performing long operations on Outlook objects:
private void longRunningProcess()
{
int max = 100;
DlgStatus dlgstatus = new DlgStatus();
dlgstatus.ProgressMax = max;
dlgstatus.Show();
for (int i = 0; i < max; i++)
{
//Execute long running code that MUST best run in the main (Outlook's) thread of execution...
System.Threading.Thread.Sleep(1000); //for simulation purposes
if (dlgstatus.Cancelled) break;
dlgstatus.SetProgress("Processing item: " + i.ToString(), i);
}
}
Here's the code for the simple progress dialog window:
public partial class DlgStatus : Form
{
private bool _cancelled;
public DlgStatus()
{
InitializeComponent();
}
public int ProgressMax
{
set
{
progress.Maximum = value;
Application.DoEvents();
}
}
public bool Cancelled
{
get { return _cancelled; }
}
public void SetProgress(string status, int val)
{
lblStatus.Text = status;
progress.Value = val;
Application.DoEvents(); //Seems to hang here
}
private void btnCancel_Click(object sender, EventArgs e)
{
_cancelled = true;
Application.DoEvents();
this.Visible = false;
}
}