473,385 Members | 1,192 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,385 software developers and data experts.

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.Invoke 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 1974
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**************@TK2MSFTNGP11.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.Invoke 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**************@TK2MSFTNGP11.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.Invoke 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.Position = Value

The UserControl2 Position property contains

UserControl1.Position = Value

Finally, the UserControl1 Position property has

If ProgressBar1.InvokeRequired 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.InvokeRequired 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.InvokeRequired Then
' Use delegate and Invoke to set value
Else
' Set property directly
UserControl1.Position = Value
End If

or am I ok with what I have?

Charles
"AlexS" <sa***********@SPAMsympaticoPLEASE.ca> wrote in message
news:%2****************@TK2MSFTNGP11.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**************@TK2MSFTNGP11.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.Invoke 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******************@TK2MSFTNGP09.phx.gbl...
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**************@TK2MSFTNGP11.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.Invoke 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$**************@TK2MSFTNGP10.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******************@TK2MSFTNGP09.phx.gbl...
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**************@TK2MSFTNGP11.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.Invoke 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***************@TK2MSFTNGP12.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$**************@TK2MSFTNGP10.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******************@TK2MSFTNGP09.phx.gbl...
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**************@TK2MSFTNGP11.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.Invoke 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.Invoke) 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***@bangproject.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***@bangproject.com> wrote in message
news:eB**************@TK2MSFTNGP09.phx.gbl...
The thread saftey issues of your private field aside, the only time you need to use the Main Thread (UserControl1.Invoke) 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***@bangproject.com for Information on Our Architecture and
Mentoring Services

</shameless self promotion>

Jul 21 '05 #10
Yes. Thread saftey has a number of its own issues, but ignore those for
now.

Until Longhorn, WinForms is built on top of the Win32 API, and there all
messages sent to the Win32 API need to be done with the owner thread, or for
us the main thread in the application. It owns all the controls. In .NET
2.0, and exception is raised anytime this is not done. For now, I use the
rule of thumb, that if it does anything remotly graphical with the UI, I use
Invoke.

--
Eric Marvets
Principal Consultant

the bang project

<shameless self promotion>

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

</shameless self promotion>
Jul 21 '05 #11

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

Similar topics

5
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...
6
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...
4
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...
2
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...
7
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...
10
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...
6
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.