Updating a windows forms control from a thread  
Author Message
c_brotherton





PostPosted: Mon Dec 27 12:11:35 CST 2004 Top

Net Framework >> Updating a windows forms control from a thread 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

DotNet75  
 
 
Herfried





PostPosted: Mon Dec 27 12:11:35 CST 2004 Top

Net Framework >> Updating a windows forms control from a thread "DW" <EMail@HideDomain.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/>


 
 
Picho





PostPosted: Mon Dec 27 12:13:02 CST 2004 Top

Net Framework >> 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" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> 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
>


 
 
DW





PostPosted: Mon Dec 27 12:17:34 CST 2004 Top

Net Framework >> 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" <EMail@HideDomain.com> wrote in message
news:%238OE1%EMail@HideDomain.com...
> Updating the GUI from a working thread is a big no-no...
>
> (thats just me quoting someone here)
>
>
> Picho
>
> "DW" <EMail@HideDomain.com> wrote in message
> news:EMail@HideDomain.com...
>> 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
>>
>
>


 
 
Picho





PostPosted: Mon Dec 27 12:38:48 CST 2004 Top

Net Framework >> 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" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> 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" <EMail@HideDomain.com> wrote in message
> news:%238OE1%EMail@HideDomain.com...
>> Updating the GUI from a working thread is a big no-no...
>>
>> (thats just me quoting someone here)
>>
>>
>> Picho
>>
>> "DW" <EMail@HideDomain.com> wrote in message
>> news:EMail@HideDomain.com...
>>> 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





PostPosted: Mon Dec 27 15:08:25 CST 2004 Top

Net Framework >> Updating a windows forms control from a thread "DW" <EMail@HideDomain.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/>

 
 
SPG





PostPosted: Mon Dec 27 15:25:57 CST 2004 Top

Net Framework >> 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]" <EMail@HideDomain.com> wrote in message
news:O3WuA%EMail@HideDomain.com...
> "DW" <EMail@HideDomain.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/>
>


 
 
DW





PostPosted: Mon Dec 27 16:24:59 CST 2004 Top

Net Framework >> 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]" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> "DW" <EMail@HideDomain.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/>


 
 
Picho





PostPosted: Mon Dec 27 16:37:07 CST 2004 Top

Net Framework >> Updating a windows forms control from a thread Herfried what do you say about my event drivven solution?


"Herfried K. Wagner [MVP]" <EMail@HideDomain.com> wrote in message
news:EMail@HideDomain.com...
> "DW" <EMail@HideDomain.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/>


 
 
Herfried





PostPosted: Mon Dec 27 17:01:54 CST 2004 Top

Net Framework >> Updating a windows forms control from a thread "Picho" <EMail@HideDomain.com> 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/>