Is there a way to invoke a method in another thread form current thread?

V

Vladimir

May be it's not obvious what am i asking for...
I have 2 threads in my application.
The first thread responds for the user interface and sending key
combinations,
the second thread listening keyboard messages from keyboard hook. There is a
report in interface, and i need to send incoming key combinations to it.
But adding items in visual report is not appropriate by the second (hook)
thread.

I know, i can use asynchronous delegate to send message to report, but in
that case this will be done by thread that is not equals interface thread.
Can i just call report method, but in interface thread?

But i need it not only in forms... There is a queue, the hook thread is
enqueuing messages to it. And i need to ask a second thread process it. How
can i do this?
 
V

Vladimir

If both threads are running in the same process then you can simply use
a synchronised queue object that are both accessible by both thread
(that if you want to use queue).

But how can i inform another thread that there is new data queue?

I know, i can use timer in another thread, but is very bad. I need very fast
reaction, and i don't want to overload cpu with timer with 1 millisecond
interval.

By the way, both thread is running through Application.Run() (in
windows message loop).
 
V

Victor Hadianto

You can try the following:

a) Poll the queue

b) Sleep the thread A and wake the thread A when thread B insert an
object in the queue. You can use the Monitor object to achieve this.

c) Have a look ThreadPool.QueueUserWorkItem(); That maybe enough for
what you're trying to achieve.



Victor Hadianto
http://www.hadianto.net/destin ation
 
V

Vladimir

a) Poll the queue
b) Sleep the thread A and wake the thread A when thread B insert an
object in the queue. You can use the Monitor object to achieve this.

c) Have a look ThreadPool.QueueUserWorkItem(); That maybe enough for
what you're trying to achieve.

This is not acceptable.
Both thread must be alive.

Listening thread have a hook registered in it.
Interface thread is displaying user interface.

Both of them in window message loop, after registering hook in first thread
i just run Application.Run(). After preparations in second thread i also run
Application.Run(). Method Application.Run() never returns until
Application.Exit() method called.

I need a mechanism such as for example hook do, i have registered callback
function to system, and the system callback to my thread (this function is
not performed by the some system thread). And i need something like this.
Just call a function in second thread from first, when the first thread will
be in some fit state for it.
 
V

Vladimir

You can try the following:
a) Poll the queue

b) Sleep the thread A and wake the thread A when thread B insert an
object in the queue. You can use the Monitor object to achieve this.

c) Have a look ThreadPool.QueueUserWorkItem(); That maybe enough for
what you're trying to achieve.

And i can't use thread pool because i must run code that can show some form
for
example. So i must run it from thread that's have a window message loop and
runs forever when application is running - for example my second thread.
 

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