473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Beginner's Question about Threading

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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #1
5 2023
Hi,

You should not need to call DoEvents() , nor need to use BeginInvoke , a
simple Invoke will do in this case.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #2
Ignacio,

Does the rest of the code look okay to you?

In my reading, it was generally recommended to use BeginInvoke to make it
asynchronous whereas Invoke would block until the message was posted. Or is
this not the case?

--
Robert W.
Vancouver, BC
www.mwtech.com

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

You should not need to call DoEvents() , nor need to use BeginInvoke , a
simple Invoke will do in this case.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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?

--
Robert W.
Vancouver, BC
www.mwtech.com


Nov 17 '05 #3

FYI - you might want to watch for any replies to my posted question "Firing
cross thread events"

I typically have my worker threads pass status & data to my GUI threads via
custom lightning bolt events...

However be careful when firing your event across threads. If the recipient
of the event is any derivative of the Control class - including a Form - then
the recipient's window handle MUST be valid when you Invoke or else your
worker thread will throw an "invalid window handle" exception...
"Robert W." wrote:
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #4
Richard,

One thing I know is an ABSOLUTE fact is that you can use 'InvokeRequired' to
test whether or not you can safely execute the "regular" code in a method.
If 'InvokeRequired' returns {True} then you can NOT excecute the code
directly but must use 'Invoke' or 'BeginInvoke'.

--
Robert W.
Vancouver, BC
www.mwtech.com

"Richard" wrote:

FYI - you might want to watch for any replies to my posted question "Firing
cross thread events"

I typically have my worker threads pass status & data to my GUI threads via
custom lightning bolt events...

However be careful when firing your event across threads. If the recipient
of the event is any derivative of the Control class - including a Form - then
the recipient's window handle MUST be valid when you Invoke or else your
worker thread will throw an "invalid window handle" exception...
"Robert W." wrote:
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?

--
Robert W.
Vancouver, BC
www.mwtech.com

Nov 17 '05 #5
Robert,

You are correct. I think BeginInvoke might be better in your case. If
you use Invoke then your worker thread will block until the UI has
finished processing the message. That means the worker thread will
have to wait until all other queued messages are processed as well.

Brian

Robert W. wrote:
Ignacio,

Does the rest of the code look okay to you?

In my reading, it was generally recommended to use BeginInvoke to make it
asynchronous whereas Invoke would block until the message was posted. Or is
this not the case?

--
Robert W.
Vancouver, BC
www.mwtech.com


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lee Garrington | last post by:
Hello, I am having a bit of a problem with multithreading in C++. After reading through 2 tutorials and implementing it in what looks the same way, it still does not compile. Basically, how do...
8
by: Shamrokk | last post by:
My application has a loop that needs to run every 2 seconds or so. To acomplish this I used... "Thread.Sleep(2000);" When I run the program it runs fine. Once I press the button that starts the...
12
by: Blaze | last post by:
I am doing the first walk through on the Visual Studio .Net walkthrough book to learn a little about programming. I am having issues with the first tutorial not running correctly. It seems that...
22
by: ddg_linux | last post by:
I have been reading about and doing a lot of php code examples from books but now I find myself wanting to do something practical with some of the skills that I have learned. I am a beginner php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.