473,621 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it necessary to use Invoke for User Controls from a Worker Thread?

I have a user control created on the main thread. Let's say, for arguments
sake, that it has a single property that maintains a private variable. If I
want to set that property from a worker thread, do I need to use
UserControl1.In voke to set it, or can I just set it? After all, it is only
changing a private variable.

TIA

Charles
Jul 21 '05 #1
10 1998
Hi, Charles

if variable is control - answer is you must use Invoke. If it is some object
non-related to UI, you can simply set it using lock statement. Lock -
because even if it is private variable if you have more than 1 thread you
might have synchronization problems.

You might want to check lock statement and volatile modifier in .Net
documentation to get more details.

HTH
Alex

"Charles Law" <bl***@nowhere. com> wrote in message
news:OJ******** ******@TK2MSFTN GP11.phx.gbl...
I have a user control created on the main thread. Let's say, for arguments
sake, that it has a single property that maintains a private variable. If I want to set that property from a worker thread, do I need to use
UserControl1.In voke to set it, or can I just set it? After all, it is only
changing a private variable.

TIA

Charles

Jul 21 '05 #2


----- Charles Law wrote: ----

After all, it is only changing a private variable

Famous last words
Jul 21 '05 #3
If you ignore thread-safety issues, the answer is no, you don't need to use
Invoke to update private fields from another thread. If you are actually
updating the screen, or perform an operation which directly or indirectly
calls an API that is thread sensitive, then you need to use Invoke.

"Charles Law" <bl***@nowhere. com> wrote in message
news:OJ******** ******@TK2MSFTN GP11.phx.gbl...
I have a user control created on the main thread. Let's say, for arguments
sake, that it has a single property that maintains a private variable. If I want to set that property from a worker thread, do I need to use
UserControl1.In voke to set it, or can I just set it? After all, it is only
changing a private variable.

TIA

Charles

Jul 21 '05 #4
Hi Alex

I'll come clean. I fibbed. I am updating a control, but I use Invoke for
that. What I actually have is a user control (UserControl1) that combines a
progress bar and a track bar. The track bar is used to set a value for the
progress bar, and when this happens the control raises an event to say that
it has happened.

This user control is placed on another user control (UserControl2), which
has a property 'Position'. This parent user control is then placed on a
form. The form also has a Position property.

The worker (background) thread can change the position as well, by

Form1.Position = 50

The form level Position property contains

UserControl2.Po sition = Value

The UserControl2 Position property contains

UserControl1.Po sition = Value

Finally, the UserControl1 Position property has

If ProgressBar1.In vokeRequired Then
' Use delegate and Invoke to set value
Else
' Set property directly
End If

As you will see, I have only used Invoke at the final stage, even though, if
I test UserControl2.In vokeRequired for example, at the higher level, I find
that it is True.

Does this mean that I should have, at the form level

If UserControl2.In vokeRequired Then
' Use delegate and Invoke to set value
Else
' Set property directly
UserControl1.Po sition = Value
End If

or am I ok with what I have?

Charles
"AlexS" <sa***********@ SPAMsympaticoPL EASE.ca> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
Hi, Charles

if variable is control - answer is you must use Invoke. If it is some object non-related to UI, you can simply set it using lock statement. Lock -
because even if it is private variable if you have more than 1 thread you
might have synchronization problems.

You might want to check lock statement and volatile modifier in .Net
documentation to get more details.

HTH
Alex

"Charles Law" <bl***@nowhere. com> wrote in message
news:OJ******** ******@TK2MSFTN GP11.phx.gbl...
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If
I
want to set that property from a worker thread, do I need to use
UserControl1.In voke to set it, or can I just set it? After all, it is

only changing a private variable.

TIA

Charles


Jul 21 '05 #5
Hi David

Thanks for the response. I have elaborated in my reply to Alex. Do you think
this changes anything? I don't want to ignore thread safety issues.

Charles
"David Levine" <no************ ****@wi.rr.com> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
If you ignore thread-safety issues, the answer is no, you don't need to use Invoke to update private fields from another thread. If you are actually
updating the screen, or perform an operation which directly or indirectly
calls an API that is thread sensitive, then you need to use Invoke.

"Charles Law" <bl***@nowhere. com> wrote in message
news:OJ******** ******@TK2MSFTN GP11.phx.gbl...
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If
I
want to set that property from a worker thread, do I need to use
UserControl1.In voke to set it, or can I just set it? After all, it is

only changing a private variable.

TIA

Charles


Jul 21 '05 #6
I didn't examine your code so I am not commenting on the specifics of what
you've done. The basic rule is actually quite simple: if setting a value
causes an operation to occur that is thread sensitive, then you must ensure
that you set the value while running on the correct thread. If all it is
doing is changing a simple field and there are no side effects, then it
should be safe to do so.

If the value you set is actually property setter, then you are doing more
then changing the value of a field, you are running code of unknown
complexity and which is potentially unbounded. Unless you are sure that the
code in the property setter will itself take care of threading issues, you
should do so before you call it. For example, if changing the property
causes events to get fired, then you could have a lot of problems if this
occurred on the wrong thread.
"Charles Law" <bl***@nowhere. com> wrote in message
news:O$******** ******@TK2MSFTN GP10.phx.gbl...
Hi David

Thanks for the response. I have elaborated in my reply to Alex. Do you think this changes anything? I don't want to ignore thread safety issues.

Charles
"David Levine" <no************ ****@wi.rr.com> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
If you ignore thread-safety issues, the answer is no, you don't need to

use
Invoke to update private fields from another thread. If you are actually
updating the screen, or perform an operation which directly or indirectly
calls an API that is thread sensitive, then you need to use Invoke.

"Charles Law" <bl***@nowhere. com> wrote in message
news:OJ******** ******@TK2MSFTN GP11.phx.gbl...
I have a user control created on the main thread. Let's say, for

arguments sake, that it has a single property that maintains a private variable. If
I
want to set that property from a worker thread, do I need to use
UserControl1.In voke to set it, or can I just set it? After all, it is

only changing a private variable.

TIA

Charles



Jul 21 '05 #7
Hi David

Thanks for the rule. It has got me thinking.
For example, if changing the property
causes events to get fired, then you could have a lot of problems if this
occurred on the wrong thread.
You may have hit on something here. I'm away to check the code to see what
is actually happening.

Charles
"David Levine" <no************ ****@wi.rr.com> wrote in message
news:%2******** *******@TK2MSFT NGP12.phx.gbl.. . I didn't examine your code so I am not commenting on the specifics of what
you've done. The basic rule is actually quite simple: if setting a value
causes an operation to occur that is thread sensitive, then you must ensure that you set the value while running on the correct thread. If all it is
doing is changing a simple field and there are no side effects, then it
should be safe to do so.

If the value you set is actually property setter, then you are doing more
then changing the value of a field, you are running code of unknown
complexity and which is potentially unbounded. Unless you are sure that the code in the property setter will itself take care of threading issues, you
should do so before you call it. For example, if changing the property
causes events to get fired, then you could have a lot of problems if this
occurred on the wrong thread.
"Charles Law" <bl***@nowhere. com> wrote in message
news:O$******** ******@TK2MSFTN GP10.phx.gbl...
Hi David

Thanks for the response. I have elaborated in my reply to Alex. Do you

think
this changes anything? I don't want to ignore thread safety issues.

Charles
"David Levine" <no************ ****@wi.rr.com> wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
If you ignore thread-safety issues, the answer is no, you don't need to
use
Invoke to update private fields from another thread. If you are
actually updating the screen, or perform an operation which directly or indirectly calls an API that is thread sensitive, then you need to use Invoke.

"Charles Law" <bl***@nowhere. com> wrote in message
news:OJ******** ******@TK2MSFTN GP11.phx.gbl...
> I have a user control created on the main thread. Let's say, for

arguments
> sake, that it has a single property that maintains a private

variable. If
I
> want to set that property from a worker thread, do I need to use
> UserControl1.In voke to set it, or can I just set it? After all, it
is only
> changing a private variable.
>
> TIA
>
> Charles
>
>



Jul 21 '05 #8
The thread saftey issues of your private field aside, the only time you need
to use the Main Thread (UserControl1.I nvoke) is for anything that will
update the UI. Its all about the painting. If 2 threads do something that
cause the form to repaint itself, it will take a big crap on you. If this
private member you are updating causes an event, the event is executed with
that thread. If that event changes a status bar on the form, then you gotta
move it over to the control's main thread.

I suggest in your private member, you use Invoke to fire the event from the
main thread.
--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

Email sa***@bangproje ct.com for Information on Our Architecture and
Mentoring Services

</shameless self promotion>
Jul 21 '05 #9
Hi Eric

You mention the painting issue, and now you've got me thinking. I use Invoke
for controls, but I don't use it for 'painting' as such. I have code in the
OnPaint override, and I read that the graphics object passed in was thread
safe, so I use stuff like DrawRectangle without using Invoke. Are you saying
that I need to use Invoke even when using the graphics method to draw
things?

Charles
"Eric Marvets" <er***@bangproj ect.com> wrote in message
news:eB******** ******@TK2MSFTN GP09.phx.gbl...
The thread saftey issues of your private field aside, the only time you need to use the Main Thread (UserControl1.I nvoke) is for anything that will
update the UI. Its all about the painting. If 2 threads do something that cause the form to repaint itself, it will take a big crap on you. If this
private member you are updating causes an event, the event is executed with that thread. If that event changes a status bar on the form, then you gotta move it over to the control's main thread.

I suggest in your private member, you use Invoke to fire the event from the main thread.
--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

Email sa***@bangproje ct.com for Information on Our Architecture and
Mentoring Services

</shameless self promotion>

Jul 21 '05 #10

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

Similar topics

5
8102
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker thread is aborted by the system. How is one to keep the background thread from calling form.Invoke after the form's window handle has been destroyed? This is definitely happening in my application. I haven't read anything about this problem in...
6
31863
by: Peter Rilling | last post by:
Okay, I have the main thread which does all the work. This main thread spawns a worker thread that just periodically poles the environment looking for a certain condition. This second thread is very small, and only is responsible for raising a flag when the environment changes. What I want is for the main thread to raise an event when the change has been flagged. I other words, I want the main thread to raise the event, not this small...
4
3568
by: Charles Law | last post by:
Hi guys. I have two threads: a main thread and a background thread. Lots of stuff happens in the background thread that means I have to update several (lots) of controls on a form. It is quite tiresome to have to write code to call MyControl.Invoke for each control on the form, along with the delegates that are required for each. Is there a better way to do this? What I mean is, if I could marshal the
2
2608
by: Lucvdv | last post by:
To avoid a temporarily frozen user interface, I'm using a separate thread to fill a list with items found in a database (there can be from a few up to about 1000 or 1500 items). There seems to be a problem in this: if the form containing the list is closed before the worker thread has finished, the Invoke call used to add entries to the list never returns. I'm trying to avoid it in any way I can imagine, but somehow it keeps getting...
7
2119
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it waits for the worker thread to complete by means of a while t.isAlive, sleep(0) mechanism. But when my worker thread calls my UpdateProgressBar routine, which calls Me.Invoke, the invoke call blocks forever. But I can't figure out why the main...
10
297
by: Charles Law | last post by:
I have a user control created on the main thread. Let's say, for arguments sake, that it has a single property that maintains a private variable. If I want to set that property from a worker thread, do I need to use UserControl1.Invoke to set it, or can I just set it? After all, it is only changing a private variable. TIA Charles
6
2811
by: k.mellor | last post by:
Hi, I hope someone can help. I have written a simple form to demonstrate my problem/question. The code follows. The form starts a thread, which using delegates updates a label (Every second adds another dot to the label). This works great. However, when I put the GUI thread to sleep (Thread.Sleep), the thread seems to stall. At first I was expecting dots to still appear, but obviously as the GUI thread is asleep, they will not. ...
0
8213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8653
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8597
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8306
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2587
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1460
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.