473,569 Members | 2,542 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling a main thread method from a worker thread

For example,

void WorkerMethod() { ... UnregisterAllHo tkeys();}
void UnregisterAllHo tKeys { for(...) {UnregisterHotK ey(...);}}

UnregisterHotKe y 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 UnregisterHotKe y function frees a hot
key previously registered by the calling thread.") Is there any way to
call UnregisterAllHo tkeys, 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 9886
I should add that I /can/ call UnregisterAllHo tkeys from WorkerMethod,
but then UnregisterHotke y 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 UnregisterAllHo tkeys from WorkerMethod,
but then UnregisterHotke y 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.co m>
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 - ISynchonrizeInv oke - 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 UnregisterAllHo tKeys
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
11746
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 performs the query is contained in a delegate function that I execute via a second thread. On 1 or 2 of the 600+ servers the query hangs. I've tried to...
4
2354
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 text to a richtextbox named txtMessageBody. 2. Make sure that the scroll is to the bottom of the box. 3. Put the focus back on the box where the user...
1
1608
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: Questions: 1. The Public Sub New proceedure. Is that just a proceedure named new or is there more to this? I went through the code and I do...
1
1405
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. chadm@predictiveconcepts.com
2
4130
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. The worker thread instantiates an object and calls methods of that object. If some method call fails, the main thread needs to be notified about the...
6
5980
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 entire operation. I could request abort on the worker thread, but that is a) potentially messy, and b) not guaranteed to take immediate effect...
1
6170
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 Pros and Cons of the approach. My logic has been that UI is always executed on the main application thread. I use Control.InvokeRequired or...
12
3528
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 thread to update some controls. But I'd like to do that asynchronously so that worker thread would not block until main thread finished updating and...
3
4695
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 application. Furthermore, I use a pointer to an unmanaged function to jump back into the managed dll. The managed part is basically a remoting...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6284
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.