473,402 Members | 2,055 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,402 software developers and data experts.

Updating a windows forms control from a thread

DW
I've gotten this question a couple of times in interviews and I don't know
what they are looking for:

How do you update a control's property, such as a textbox.text property,
from a thread, in .NET? It seems to me you just update it normally. What
are the interviewers looking for here? Thanks.

- W
Nov 16 '05 #1
11 2192
"DW" <dd********@hotmail.com> schrieb:
I've gotten this question a couple of times in interviews and I don't know
what they are looking for:

How do you update a control's property, such as a textbox.text property,
from a thread, in .NET? It seems to me you just update it normally. What
are the interviewers looking for here?


Always create your forms in your application's main UI thread. Instance
members of Windows Forms forms and controls are not safe for multithreading,
so accessing them directly can cause problems. In addition to that, your
forms need a message pump which is normally provided by the app's main UI
thread.

More information:

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 16 '05 #2
Updating the GUI from a working thread is a big no-no...

(thats just me quoting someone here)
Picho

"DW" <dd********@hotmail.com> wrote in message
news:Bc********************@rcn.net...
I've gotten this question a couple of times in interviews and I don't know
what they are looking for:

How do you update a control's property, such as a textbox.text property,
from a thread, in .NET? It seems to me you just update it normally. What
are the interviewers looking for here? Thanks.

- W

Nov 16 '05 #3
DW
Thanks Picho. Then specifically, how would you do that? Could you provide
a small code example. I've been looking for one unsuccessfully. Thanks
again.

- DW
"Picho" <SP********@telhai.ac.il> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Updating the GUI from a working thread is a big no-no...

(thats just me quoting someone here)
Picho

"DW" <dd********@hotmail.com> wrote in message
news:Bc********************@rcn.net...
I've gotten this question a couple of times in interviews and I don't
know what they are looking for:

How do you update a control's property, such as a textbox.text property,
from a thread, in .NET? It seems to me you just update it normally.
What are the interviewers looking for here? Thanks.

- W


Nov 16 '05 #4
Well, I am no expert but...

I have this good book by Chris Sells - " Windows Forms Programming in C#".
there is a whole chapter discussing multi-threaded UI, and how shared data
is to be accessed using monitors.

personally (I might get banned from this group for my bad habbits...), I
usually raise events from the worker thread that have handlers in the UI
thread that reflects changes in the UI.

an example can be provided if you provide a skeletal context of your
specific problem.

Picho.
"DW" <dd********@hotmail.com> wrote in message
news:ia********************@rcn.net...
Thanks Picho. Then specifically, how would you do that? Could you
provide a small code example. I've been looking for one unsuccessfully.
Thanks again.

- DW
"Picho" <SP********@telhai.ac.il> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
Updating the GUI from a working thread is a big no-no...

(thats just me quoting someone here)
Picho

"DW" <dd********@hotmail.com> wrote in message
news:Bc********************@rcn.net...
I've gotten this question a couple of times in interviews and I don't
know what they are looking for:

How do you update a control's property, such as a textbox.text property,
from a thread, in .NET? It seems to me you just update it normally.
What are the interviewers looking for here? Thanks.

- W



Nov 16 '05 #5
"DW" <dd********@hotmail.com> schrieb:
Thanks Picho. Then specifically, how would you do that?


'Control.Invoke'/'Control.BeginInvoke'. You'll find more information in my
other reply.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 16 '05 #6
SPG
Herfried,

We have a problem based on updating the UI from worker threads..
Our app uses a docking style MDI UI, where each form is capable of receiving
updates from a central "controller", as well as performing form specific
tasks.
We use the Control.Invoke() lark, which of course works, but when we are
making updates to many forms (We could have a form showing a graph, one a
table, one a report etc.. all based on their own interpretation fo the data
being pumped out) we find that the UI becomes very slow, sticky and
unmanageable.

Is is possible to assign each form its own UI thread/message pump so they
can all act independantly of each other?
Steve

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O3****************@TK2MSFTNGP10.phx.gbl...
"DW" <dd********@hotmail.com> schrieb:
I've gotten this question a couple of times in interviews and I don't
know what they are looking for:

How do you update a control's property, such as a textbox.text property,
from a thread, in .NET? It seems to me you just update it normally.
What are the interviewers looking for here?


Always create your forms in your application's main UI thread. Instance
members of Windows Forms forms and controls are not safe for
multithreading,
so accessing them directly can cause problems. In addition to that, your
forms need a message pump which is normally provided by the app's main UI
thread.

More information:

Multithreading in Windows Forms applications
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 16 '05 #7
DW
Thank you to Herfried and Picho. Now I understand how to do this.

- David

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O6**************@TK2MSFTNGP09.phx.gbl...
"DW" <dd********@hotmail.com> schrieb:
Thanks Picho. Then specifically, how would you do that?


'Control.Invoke'/'Control.BeginInvoke'. You'll find more information in
my other reply.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 16 '05 #8
Herfried what do you say about my event drivven solution?
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:O6**************@TK2MSFTNGP09.phx.gbl...
"DW" <dd********@hotmail.com> schrieb:
Thanks Picho. Then specifically, how would you do that?


'Control.Invoke'/'Control.BeginInvoke'. You'll find more information in
my other reply.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 16 '05 #9
"Picho" <SP********@telhai.ac.il> schrieb:
Herfried what do you say about my event drivven solution?


In general, event driven solutions are not thread-safe too, but this depends
on the implementation of the event. More information:

<URL:http://www.google.de/groups?selm=%23XkWlYOuCHA.1656%40TK2MSFTNGP09>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 16 '05 #10
Theres no "in general" about it - it suggests there are situations where it is OK. Raising an event from the worker thread executes code on the worker thread that is part of the UI. YOU MUST NOT DO THIS. The mostr frustrating thing is that the WInforms classes don't tell you this ... until the next version, where in debug they will throw an exception.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Picho" <SP********@telhai.ac.il> schrieb:
Herfried what do you say about my event drivven solution?


In general, event driven solutions are not thread-safe too, but this depends
on the implementation of the event. More information:

<URL:http://www.google.de/groups?selm=%23XkWlYOuCHA.1656%40TK2MSFTNGP09>
Nov 16 '05 #11
To my understanding, the non-thread-safe operations are the += and -= of the
event.
what if I make sure these only happen before I start the worker thread and
after it finishes working?
"Richard Blewett [DevelopMentor]" <ri******@NOSPAMdevelop.com> wrote in
message news:uk**************@TK2MSFTNGP15.phx.gbl...
Theres no "in general" about it - it suggests there are situations where
it is OK. Raising an event from the worker thread executes code on the
worker thread that is part of the UI. YOU MUST NOT DO THIS. The mostr
frustrating thing is that the WInforms classes don't tell you this ...
until the next version, where in debug they will throw an exception.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Picho" <SP********@telhai.ac.il> schrieb:
Herfried what do you say about my event drivven solution?


In general, event driven solutions are not thread-safe too, but this
depends
on the implementation of the event. More information:

<URL:http://www.google.de/groups?selm=%23XkWlYOuCHA.1656%40TK2MSFTNGP09>

Nov 16 '05 #12

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

Similar topics

1
by: Ron James | last post by:
I have a Form based dialog which kicks off a worker thread. The form has a progress bar, and a Cancel button. The Cancel button Aborts the thread, and when exiting, the thread attempts to Show a...
5
by: Claire | last post by:
My progress window is created by a secondary thread and then updated by it while a file is uploaded. There's an avi animation control on there that should show the move file avi. Plus a progress...
0
by: willow1480 | last post by:
I am developing a small little Service Control Application. I am using a listview control with checkboxes and getting the list of services I want to control from a text file. When you check a...
2
by: BG | last post by:
We're having trouble writing the code to update a UI control (label.Text) from a secondary thread. We're using C# with Windows Forms. We have a main form named MainForm, a splash screen form...
8
by: GTi | last post by:
I have a UI Class with StatusBarPanels Then I have another Class that is a thread. I want this class thread to update the StatusBarPanels in the UI class. But I get the error: error CS0120: An...
9
by: Jervin Justin | last post by:
Hi, I've been having this problem for some time with Web Forms: I have a web app that sends data to a service when the user presses a button. Based on the data, the server will send several...
1
by: K Viltersten | last post by:
> bar.PerformStep (); I noticed that there's a property for the bar stating what time it should take for the animation to move the fill to the requested spot. I'm guessing that's the time i...
1
by: jonbartlam | last post by:
Hi There I'm not sure what exactly is going wrong here. I'm writing an application that retreives a table from a database (tbl_internalfaults) and updates it. (Actually, just the status column will...
4
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i have a main thread an another worker thread. The main Thread creates another thread and waits for the threads signal to continue the main thread. Everything works inside a ModalDialog and...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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
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
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,...

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.