Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2004

Tip: Looking for answers? Try searching our database.

Updating a windows forms control from a thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DW - 27 Dec 2004 18:07 GMT
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
Herfried K. Wagner [MVP] - 27 Dec 2004 18:11 GMT
"DW" <dddddwwwww@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>

Signature

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

SPG - 27 Dec 2004 21:25 GMT
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

> "DW" <dddddwwwww@hotmail.com> schrieb:
>> I've gotten this question a couple of times in interviews and I don't
[quoted text clipped - 15 lines]
> Multithreading in Windows Forms applications
> <URL:http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en>
Picho - 27 Dec 2004 18:13 GMT
Updating the GUI from a working thread is a big no-no...

(thats just me quoting someone here)

Picho

> I've gotten this question a couple of times in interviews and I don't know
> what they are looking for:
[quoted text clipped - 4 lines]
>
> - W
DW - 27 Dec 2004 18:17 GMT
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
> Updating the GUI from a working thread is a big no-no...
>
[quoted text clipped - 10 lines]
>>
>> - W
Picho - 27 Dec 2004 18:38 GMT
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.

> Thanks Picho.  Then specifically, how would you do that?  Could you
> provide a small code example.  I've been looking for one unsuccessfully.
[quoted text clipped - 15 lines]
>>>
>>> - W
Herfried K. Wagner [MVP] - 27 Dec 2004 21:08 GMT
"DW" <dddddwwwww@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.

Signature

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

DW - 27 Dec 2004 22:25 GMT
Thank you to Herfried and Picho.  Now I understand how to do this.

- David

> "DW" <dddddwwwww@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.
Picho - 27 Dec 2004 22:37 GMT
Herfried what do you say about my event drivven solution?

> "DW" <dddddwwwww@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.
Herfried K. Wagner [MVP] - 27 Dec 2004 23:01 GMT
"Picho" <SPAM_picho@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>

Signature

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

Richard Blewett [DevelopMentor] - 28 Dec 2004 00:06 GMT
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" <SPAM_picho@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
Picho - 28 Dec 2004 05:14 GMT
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?

> 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
[quoted text clipped - 16 lines]
>
> <URL:http://www.google.de/groups?selm=%23XkWlYOuCHA.1656%40TK2MSFTNGP09

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.