473,480 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Cross-thread GUI call

Hi everyone,

Is it safe to call a GUI control's members directly from another thread.
That is, if I launch a background thread when the user clicks a button on my
form, can I safely retrieve info from one of its controls on that thread. I
assume that behind the scenes the call will occur back on the GUI thread
which should be fine since I have a "Work in progress" dialog running at the
time (so its message pump can handle the cross-thread call accordingly).
AFAIK then, synchronization shouldn't be an issue under these circumstances.
Can someone confirm this is safe. Thanks for your help.
Oct 20 '06 #1
3 2411
OK... no.

If you aren't on the GUI thread, you cannot do this. Just because your
user can't do anything doesn't change it; the user is not the only
thing using that thread. In fact, only a slim minority of the events
that a form gets are directly caused by the user.

In 2.0 the implementation will (correctly) catch you doing this and
snigger at you (right before it throws an exception at your background
thread); so you must either use Invoke/BeginInvoke to get back at the
UI thread, else obtain the information you need *first* (before
starting the second thread).

Marc

Oct 20 '06 #2
It is not safe. Use the BackgroundWorker component, which uses BeginInvoke
and EndInvoke to marshal an object to the UI thread, then execute it's
parameters there, usually info to update progress bars, text, etc.

"Jake Weller" <no_spam@_nospam.comwrote in message
news:eZ**************@TK2MSFTNGP05.phx.gbl...
Hi everyone,

Is it safe to call a GUI control's members directly from another thread.
That is, if I launch a background thread when the user clicks a button on
my form, can I safely retrieve info from one of its controls on that
thread. I assume that behind the scenes the call will occur back on the
GUI thread which should be fine since I have a "Work in progress" dialog
running at the time (so its message pump can handle the cross-thread call
accordingly). AFAIK then, synchronization shouldn't be an issue under
these circumstances. Can someone confirm this is safe. Thanks for your
help.

Oct 21 '06 #3
"Chris Bordeman"
<RE*********************************************** ***@hhoottmmaaiill.com>
wrote in message news:eo**************@TK2MSFTNGP04.phx.gbl...
It is not safe. Use the BackgroundWorker component, which uses
BeginInvoke and EndInvoke to marshal an object to the UI thread, then
execute it's parameters there, usually info to update progress bars, text,
etc.
Thanks for the feedback. I already am using the "BackgroundWorker" component
but need to read data from my form on the worker thread. The form itself is
completely off-limits to the user during this time since a "work in
progress" (modal) dialog sits on top of it while my thread runs (as
mentioned earlier). You're saying this still isn't safe though? I've read
the following:

http://msdn2.microsoft.com/en-us/library/ms171728.aspx

and I'm not receiving the "InvalidOperationException" exception they refer
to. Ok, maybe it's potentially dangerous anyway (not sure which is why I'm
here) but given that I'm only reading (not writing) data from a dialog that
can't be altered by the user during this time (only by my worker thread),
you're still saying it's not safe?
Oct 23 '06 #4

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

Similar topics

0
2030
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
12
3841
by: * ProteanThread * | last post by:
but depends upon the clique: ...
3
3068
by: rollasoc | last post by:
Hi, Doing a bit of system testing on a Windows 98 laptop. (.Net 1.1 app). Did a bit of testing. Loaded a previously saved file. A gray box appeared with the text and buttons all white...
0
1858
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
4
5550
by: David Peach | last post by:
Hello, hope somebody here can help me... I have a query that lists defects recorded in a user defined date range. That query is then used as the source for a Cross Tab query that cross-tabs count...
23
6477
by: Jeff Rodriguez | last post by:
Here's what I want do: Have a main daemon which starts up several threads in a Boss-Queue structure. From those threads, I want them all to sit and watch a queue. Once an entry goes into the...
0
2034
by: Web Science | last post by:
Site and Features: http://www.eigensearch.com Search engine, eigenMethod, eigenvector, mathematical, manifolds, science, technical, search tools, eigenmath, Jacobian, quantum, mechanics,...
7
3885
by: Scott M. | last post by:
How can I disable the cross-site scripting check for one particular page of a site?
1
2742
by: Rob Woodworth | last post by:
Hi, I'm having serious problems getting my report to work. I need to generate a timesheet report which will contain info for one employee between certain dates (one week's worth of dates). I...
6
3963
by: ampo | last post by:
Hello. Can anyone help with cross-domain problem? I have HTML page from server1 that send xmlHTTPRequest to server2. How can I do it? Thanks.
0
7037
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,...
0
7032
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
7076
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...
0
6873
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...
0
5321
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,...
1
4767
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...
0
4471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1294
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 ...
0
174
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...

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.