Calling a main thread method from a worker thread

Calling a main thread method from a worker thread

Post by Hao » Tue, 03 May 2005 10:24:54


For example,

void WorkerMethod() { ... UnregisterAllHotkeys();}
void UnregisterAllHotKeys { for(...) {UnregisterHotKey(...);}}

UnregisterHotKey is an API function that must be on the thread that
RegisterHotKey was called in order to unregister any of the hot keys
RegisterHotkey registered. ("The UnregisterHotKey function frees a hot
key previously registered by the calling thread.") Is there any way to
call UnregisterAllHotkeys, a main thread method, from WorkerMethod()?
If I had a Control and was updating it, I could use
Control.(Begin)Invoke, but I don't.
 
 
 

Calling a main thread method from a worker thread

Post by Hao » Tue, 03 May 2005 10:45:38

I should add that I /can/ call UnregisterAllHotkeys from WorkerMethod,
but then UnregisterHotkey just returns a bunch of errors since it can't
find any of the hot keys registered on the main thread. My question
should be, is there a way for a worker thread method to call a main
thread method and have the main thread method execute on the main
thread?

 
 
 

Calling a main thread method from a worker thread

Post by Wavemake » Tue, 03 May 2005 12:52:28


<snip>


Since Invoke is not an option for you, another approach would be to use
the AutoResetEvent class. The main thread can use an AutoResetEvent
object to wait until it is signaled to do some work. So the worker
thread can signal the main thread. The main thread then does the work
within its own thread. Of course, whether or not you can use this
approach depends on whether or not you can allow the main thread to wait
to be signaled.
 
 
 

Calling a main thread method from a worker thread

Post by Jon Skeet » Tue, 03 May 2005 15:05:16


The main thread has to be waiting somehow for other threads to ask it
to do some work. Wavemaker suggested AutoResetEvent, but I'd suggest
using Monitor.Wait/Pulse, possibly encapsulated in a producer/consumer
queue. See the code half way down
http://www.yqcomputer.com/ ~skeet/csharp/threads/deadlocks.shtml
for an example.

--
Jon Skeet - < XXXX@XXXXX.COM >
http://www.yqcomputer.com/ ~skeet
If replying to the group, please do not mail me too
 
 
 

Calling a main thread method from a worker thread

Post by Richard Bl » Tue, 03 May 2005 15:14:56

Assuming the main thread to be a UI thread, this will freeze the UI. I assume the OP wants the main thread to continue processing and just the call to UnregistHoyKeys be marshalled on to it. And if that is the case there is a well defined mechanism for doing this - ISynchonrizeInvoke - or better known as Control.Invoke.

The qyest is why have youo hamstrung yourself with ont being able to get to the form or a control from the worker thread? Is this because "this is how I designed it" or "I don't have access to the main thread as its someone else's component" or something else?

Regards

Richard Blewett - DevelopMentor
http://www.yqcomputer.com/
http://www.yqcomputer.com/



<snip>

> My question
> should be, is there a way for a worker thread method to call a main
> thread method and have the main thread method execute on the main
> thread?

Since Invoke is not an option for you, another approach would be to use
the AutoResetEvent class. The main thread can use an AutoResetEvent
object to wait until it is signaled to do some work. So the worker
thread can signal the main thread. The main thread then does the work
within its own thread. Of course, whether or not you can use this
approach depends on whether or not you can allow the main thread to wait
to be signaled.
 
 
 

Calling a main thread method from a worker thread

Post by Hao » Wed, 04 May 2005 04:31:34

Nah, it's that I'm developing a plugin for an application so I don't
have any real controls to work with and the application passes itself
into my plugin as an object. (I have tried creating a dummy
Systems.Window.Forms.Textbox on the main thread an messing with that,
but that hasn't produced any fruition.) The main thread is an UI thread
(the application that's hosting the plugin, or at least that's how I
think its extensibility is designed), but the UnregisterAllHotKeys
method shouldn't hopefully take very long.

Thanks for all the suggestions.