"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>
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