Multi-Threading and display question

S

Stefan

Hello,

I have a Main Form on the main thread and many worker threads.
Each worker thread has custom user controls to be displayed on the main form
but controlled by the worker threads.

I want the worker thread to be able to modify it's custom user control and
at the same time have the main form display the custom user controls.

When the custom user controls are created by the worker thread, they can't
be added to the main form since they were not created on the same thread as
the main form. When I create the custom user controls on the main
form/thread, the worker threads can no longer modify it's user control.

I want to keep the worker thread and the custom controls as simple and
straight forward as possible. What is the best solution in this scenario?

Thanks
 
J

Jon Skeet [C# MVP]

Stefan said:
I have a Main Form on the main thread and many worker threads.
Each worker thread has custom user controls to be displayed on the main form
but controlled by the worker threads.

I want the worker thread to be able to modify it's custom user control and
at the same time have the main form display the custom user controls.

When the custom user controls are created by the worker thread, they can't
be added to the main form since they were not created on the same thread as
the main form. When I create the custom user controls on the main
form/thread, the worker threads can no longer modify it's user control.

I want to keep the worker thread and the custom controls as simple and
straight forward as possible. What is the best solution in this scenario?

I suggest you do as much work as possible in the worker thread, but
then when you actually need to create the controls, do that on the main
thread in the normal way (using Control.Invoke/BeginInvoke).

Again, when your worker threads want to change the state of the UI, use
Control.Invoke/BeginInvoke to execute a delegate on the UI thread.

See
http://www.pobox.com/~skeet/csharp/multithreading.html#windows.forms
for more information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top