Calling a main thread method from a worker thread

H

Hao L

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.
 
H

Hao L

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?
 
W

Wavemaker

:

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.
 
J

Jon Skeet [C# MVP]

Hao L said:
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?

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.pobox.com/~skeet/csharp/threads/deadlocks.shtml
for an example.
 
R

Richard Blewett [DevelopMentor]

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.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

:

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.
 
H

Hao L

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.
 

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