Component with threads

  • Thread starter Thread starter Steve Whine
  • Start date Start date
S

Steve Whine

Hello,

I am writing a Component whose work needs to be done on a separate
thread (or ThreadPool), and when this work is done a Component event
needs to be fired in the main GUI thread. What is the best way to send a
mesasge to this Component?

As far as I can see, here are my options, in order from least appealing
to most appealing:

1) Make my Component a Control so I can use Control.Invoke. I don't like
this idea, my control does not need to be visible at runtime.

2) Use Application.Forms and call Invoke on one of those forms. This
means that the Component won't work if someone tries to use it in a
window-less app.

3) Create a static hidden message window with my component. What would
be the best way to do that? There is a MessageWindow class but it seems
to be limited to the compact framework.

If anyone can help, please let me know!

Cheers
Steve
 
Steve Whine said:
I am writing a Component whose work needs to be done on a separate
thread (or ThreadPool), and when this work is done a Component event
needs to be fired in the main GUI thread. What is the best way to send a
mesasge to this Component?

As far as I can see, here are my options, in order from least appealing
to most appealing:

1) Make my Component a Control so I can use Control.Invoke. I don't like
this idea, my control does not need to be visible at runtime.

2) Use Application.Forms and call Invoke on one of those forms. This
means that the Component won't work if someone tries to use it in a
window-less app.

3) Create a static hidden message window with my component. What would
be the best way to do that? There is a MessageWindow class but it seems
to be limited to the compact framework.

If you already *have* a UI thread, chances are you've got at least *a*
control visible - pass that (as an ISynchronizeInvoke) to the object
doing the work.

If you aren't guaranteeing that you've got a UI thread, you need to
have something running a message pump or something similar - a
producer/consumer queue could quite possibly do what you want. See half
way down
http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml

You could provide an API which allows the user to specify an
ISynchronizeInvoke to use, so they can do the most appropriate thing to
their situation, either passing in a control or possibly their own
implementation which uses something like the queue above if necessary.
 
Jon said:
If you already *have* a UI thread, chances are you've got at least *a*
control visible - pass that (as an ISynchronizeInvoke) to the object
doing the work.

If you aren't guaranteeing that you've got a UI thread, you need to
have something running a message pump or something similar - a
producer/consumer queue could quite possibly do what you want. See half
way down
http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml

You could provide an API which allows the user to specify an
ISynchronizeInvoke to use, so they can do the most appropriate thing to
their situation, either passing in a control or possibly their own
implementation which uses something like the queue above if necessary.
Thank you Jon, I had not come across ISynchronizeInvoke, I will look
into it.
 
Back
Top