By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,915 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,915 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Aug 25 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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).

Victor Hadianto
http://www.hadianto.net/destination

Aug 25 '05 #2

P: n/a
> 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).
Aug 25 '05 #3

P: n/a
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

Aug 26 '05 #4

P: n/a
> 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.
Aug 26 '05 #5

P: n/a
> 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.
Aug 26 '05 #6

P: n/a
Vladimir,

If you can get a reference to one of the application's forms in the
second thread then you can use the BeginInvoke or Invoke from that form
to marshal the execution of a delegate onto the thread hosting that
form.

Brian

Vladimir wrote:
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?


Aug 26 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.