Connecting Tech Pros Worldwide Forums | Help | Site Map

Updating a windows forms control from a thread

DW
Guest
 
Posts: n/a
#1: Nov 16 '05
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]
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Updating a windows forms control from a thread


"DW" <dddddwwwww@hotmail.com> schrieb:[color=blue]
> 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?[/color]

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


Picho
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Updating a windows forms control from a thread


Updating the GUI from a working thread is a big no-no...

(thats just me quoting someone here)


Picho

"DW" <dddddwwwww@hotmail.com> wrote in message
news:BcidnagG9OXY0k3cRVn-ug@rcn.net...[color=blue]
> 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
>[/color]


DW
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Updating a windows forms control from a thread


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" <SPAM_picho@telhai.ac.il> wrote in message
news:%238OE1%23D7EHA.1288@TK2MSFTNGP10.phx.gbl...[color=blue]
> Updating the GUI from a working thread is a big no-no...
>
> (thats just me quoting someone here)
>
>
> Picho
>
> "DW" <dddddwwwww@hotmail.com> wrote in message
> news:BcidnagG9OXY0k3cRVn-ug@rcn.net...[color=green]
>> 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
>>[/color]
>
>[/color]


Picho
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Updating a windows forms control from a thread


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" <dddddwwwww@hotmail.com> wrote in message
news:iaednRG2Oa8jzE3cRVn-hg@rcn.net...[color=blue]
> 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" <SPAM_picho@telhai.ac.il> wrote in message
> news:%238OE1%23D7EHA.1288@TK2MSFTNGP10.phx.gbl...[color=green]
>> Updating the GUI from a working thread is a big no-no...
>>
>> (thats just me quoting someone here)
>>
>>
>> Picho
>>
>> "DW" <dddddwwwww@hotmail.com> wrote in message
>> news:BcidnagG9OXY0k3cRVn-ug@rcn.net...[color=darkred]
>>> 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
>>>[/color]
>>
>>[/color]
>
>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Updating a windows forms control from a thread


"DW" <dddddwwwww@hotmail.com> schrieb:[color=blue]
> Thanks Picho. Then specifically, how would you do that?[/color]

'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/>

SPG
Guest
 
Posts: n/a
#7: Nov 16 '05

re: Updating a windows forms control from a thread


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]" <hirf-spam-me-here@gmx.at> wrote in message
news:O3WuA%23D7EHA.2568@TK2MSFTNGP10.phx.gbl...[color=blue]
> "DW" <dddddwwwww@hotmail.com> schrieb:[color=green]
>> 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?[/color]
>
> 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/>
>[/color]


DW
Guest
 
Posts: n/a
#8: Nov 16 '05

re: Updating a windows forms control from a thread


Thank you to Herfried and Picho. Now I understand how to do this.

- David

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:O6RBzgF7EHA.2540@TK2MSFTNGP09.phx.gbl...[color=blue]
> "DW" <dddddwwwww@hotmail.com> schrieb:[color=green]
>> Thanks Picho. Then specifically, how would you do that?[/color]
>
> '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/>[/color]


Picho
Guest
 
Posts: n/a
#9: Nov 16 '05

re: Updating a windows forms control from a thread


Herfried what do you say about my event drivven solution?


"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:O6RBzgF7EHA.2540@TK2MSFTNGP09.phx.gbl...[color=blue]
> "DW" <dddddwwwww@hotmail.com> schrieb:[color=green]
>> Thanks Picho. Then specifically, how would you do that?[/color]
>
> '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/>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#10: Nov 16 '05

re: Updating a windows forms control from a thread


"Picho" <SPAM_picho@telhai.ac.il> schrieb:[color=blue]
> Herfried what do you say about my event drivven solution?[/color]

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

Richard Blewett [DevelopMentor]
Guest
 
Posts: n/a
#11: Nov 16 '05

re: Updating a windows forms control from a thread


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:[color=blue]
> Herfried what do you say about my event drivven solution?[/color]

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
Guest
 
Posts: n/a
#12: Nov 16 '05

re: Updating a windows forms control from a thread


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]" <richardb@NOSPAMdevelop.com> wrote in
message news:ukbXEEH7EHA.2016@TK2MSFTNGP15.phx.gbl...[color=blue]
> 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:[color=green]
> > Herfried what do you say about my event drivven solution?[/color]
>
> 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>
>
>[/color]


Closed Thread