473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Calling a main thread method from a worker thread

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.

Nov 17 '05 #1
5 9866
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?

Nov 17 '05 #2

"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.
Nov 17 '05 #3
Hao L <ha******@gmail.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
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.

Nov 17 '05 #5
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.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Bryan | last post by:
I have a multi-threaded C# console application that uses WMI (System.Management namespace) to make RPC calls to several servers (600+ ) and returns ScheduledJobs. The section of my code that...
4
by: Dean Bortell | last post by:
I am writing a multi-threaded program with a worker thread. I want the rich text box to auto scroll down as the text is added. Here is what I want to happen: From the worker thread: 1. Add the...
1
by: Peter | last post by:
I found this awesome example of calling a Main Thread from a worker thread on abstractvb.com but I read through the example and would like to see if anyone can clarify some of this code below: ...
1
by: Chad Miller | last post by:
I know haw to call a the main thread from a worker thread in a windows form class, but how does a worker thread call its main thread from a none windows form class? Thank You. ...
2
by: Tim | last post by:
The are 2 threads - main thread and worker thread. The main thread creates the worker thread and then the worker thread runs in an infinite while loop till it is asked to exit by setting an event....
6
by: Joe Jax | last post by:
I have an object that spawns a worker thread to process one of its methods. That method processes methods on a collection of other objects. During this processing, a user may request to cancel the...
1
by: bytebugs | last post by:
Hi, As a matter of principle I have refranied from creating an instance of Form or UserControl from within a class that does not inherit from System.Windows.Form. I am looking for comments on...
12
by: Tomaz Koritnik | last post by:
Is it possible to asynchronously call a method from worker thread so that this method would execute in main-thread? I'm doing some work in worker thread and would like to report progress to main...
3
by: Klaus | last post by:
Hi, I have an existing VC 6 MFC application which communicates asynchronly with a VC 2005 managed code dll. I use an unmanaged base class with virtual functions to access methods in the MFC...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.