G
Guest
I'm creating a WinForms app that will act as a companion (think administrator
functionality) to a Pocket PC app. Generally the WinForms app works under
just the UI thread. But if a Pocket PC connects to the desktop via
ActiveSync then a separate thread is spawned.
In addition to the main desktop app window I also have a Notification form
that appears in the lower right of the screen and provides feedback to the
user about what is happening during the Data Transfer process. At first I
instantiated this small form from the Data Transfer thread but then read
several articles and decided to also instantiate it from the UI thread.
I was having all kinds of problems sending update messages from the Data
Transfer thread to the Notification form but did some more reading and
realized that I was breaking the primary threading rule about updating a
control only from the thread where it was created. From my reading I created
methods inside the Notification form class that look like this:
private delegate void ShowStatusDelegate(string text);
public void ShowStatus(string text)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowStatusDelegate(ShowStatus), new object[]
{text});
return;
}
// Only reaches here when on UI thread
labelStatus.Text = text;
Application.DoEvents();
}
I have two immediate questions:
1. Does the code in this method look correct and "solid" ?
2. Do I need the "Application.DoEvents()" line or is it unnecessary?
functionality) to a Pocket PC app. Generally the WinForms app works under
just the UI thread. But if a Pocket PC connects to the desktop via
ActiveSync then a separate thread is spawned.
In addition to the main desktop app window I also have a Notification form
that appears in the lower right of the screen and provides feedback to the
user about what is happening during the Data Transfer process. At first I
instantiated this small form from the Data Transfer thread but then read
several articles and decided to also instantiate it from the UI thread.
I was having all kinds of problems sending update messages from the Data
Transfer thread to the Notification form but did some more reading and
realized that I was breaking the primary threading rule about updating a
control only from the thread where it was created. From my reading I created
methods inside the Notification form class that look like this:
private delegate void ShowStatusDelegate(string text);
public void ShowStatus(string text)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new ShowStatusDelegate(ShowStatus), new object[]
{text});
return;
}
// Only reaches here when on UI thread
labelStatus.Text = text;
Application.DoEvents();
}
I have two immediate questions:
1. Does the code in this method look correct and "solid" ?
2. Do I need the "Application.DoEvents()" line or is it unnecessary?