PC Review


Reply
Thread Tools Rate Thread

Calling a main thread method from a worker thread

 
 
Hao L
Guest
Posts: n/a
 
      2nd May 2005
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.

 
Reply With Quote
 
 
 
 
Hao L
Guest
Posts: n/a
 
      2nd May 2005
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?

 
Reply With Quote
 
Wavemaker
Guest
Posts: n/a
 
      2nd May 2005

"Hao L" wrote:

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


 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      2nd May 2005
Hao L <(E-Mail Removed)> wrote:
> 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/t...eadlocks.shtml
for an example.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Richard Blewett [DevelopMentor]
Guest
Posts: n/a
 
      2nd May 2005
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

"Hao L" wrote:

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

 
Reply With Quote
 
Hao L
Guest
Posts: n/a
 
      2nd May 2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Invoking a method from the main thread, from a worker thread. DaTurk Microsoft C# .NET 6 30th Aug 2007 08:07 PM
Calling main thread from a worker thread Chad Miller Microsoft VB .NET 0 13th Jan 2005 03:54 PM
Calling main thread from a worker thread Chad Miller Microsoft VB .NET 1 13th Jan 2005 03:14 PM
Worker Thread raising events to the main thread? ORC Microsoft Dot NET Compact Framework 5 24th Nov 2004 03:52 PM
Calling Main Thread from a worker thread in vb.net Documentation Peter Microsoft VB .NET 1 1st Nov 2004 07:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:19 PM.