invokes and threads and pumps...oh my..

G

Guest

I'm updating a .NET DLL that is used by a windows app.
Unfortunatly I can't change the windows app, just my DLL.
I want to run most of my code in a different thread
(for multiple reasons...) but a vendor API that my DLL
uses requires a message pump for an internal timer.

I wrote a test program to prototype this. It's an EXE (windows app)
which instantiates an object from a seperate DLL, which starts
a new thread, which then calls Application.Run(new HiddenForm()).
You get the idea. I spit out some info to the console and confirmed
that each window is running in a seperate thread and that each thread
has it's own message pump.

When I make a call from the window generated by the EXE to the window
generated by the DLL, I have to use BeginInvoke or it throws an error.
(I'm guessing the error is thrown because the DLL window is not receiving
any messages from the message pump on the first thread.)

However, if I raise an event from the DLL's window to the EXE's window, it
DOES NOT throw an error, even though the EXE window is receiving messages
from the second thread's message pump.
I thought I would have to use BeginInvoke to get the event to "jump"
over to the first thread just like I had to use BeginInvoke to get the
function call
from the EXE to "jump" to the DLL's thread.

Is the EXE window receiving messages from BOTH message pumps??
How can that be?
Am I missing something here??

Thanks!
Steve
 
F

Francisco Padron

Events do NOT go through the message queue (events are not specific to
Windows Form controls), if you call an event across threads is YOUR
responsibility that the object trapping the event is thread safe otherwise,
you need to check in your thread handler whether or not the current thread
and your Message Queue thread are the same, if they are not you must do a
BeginInvoke (to yourself).

You can use Control.InvokeRequired to find out whether the current thread
matches a control's message queue 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