as the author show us in the sample,pAdd->Add() ought to be works asychronous call,but in fact ,the method work still sychronus. then I wonder if the call of pClientSink->ExecutionOver(a+b) is usefullessness.
Add() function blocks
Posted by imhawley
on 06/25/2004 12:08pm
The reason the call isn't asynchronous is because it calls Fire_ExecutionOver() from within it's body. This brings up a dialog box, a blocking operation, the Add() function cannot return until Fire_ExecutionOver() returns (until the dialog box has been closed).
It is necessary therefore to decouple the Add() function from it's call. One method of doing this would be to place the parameters into a deque of events (with suitable locking objects attached if needed) and return immeditately.
The CoClass implementing IAdd would have then to have it's own thread, reading the parameters from the same deque, performing the necessary actions (i.e. perform the Add()) and then calling the Fire_ExecutionOver() from with it's own thread. I have to say though, I don't know if the sample is setup to allow calling the connection point across threads, but that might merely be solved by entering the multi-threaded appatment prior to creaing the IAdd interface.
Hope that helps (should you see my response!)
In my opinion, this feature of COM should act like a working thread. However, while COM's Add is working, I tried to move the dialog window - it didn't respond unitil the Add method is returned. How come?
I downloaded the example and executed the client. I do not get any message box from ExectionOver method in the Sink. Any knowledge abt why this happens?
How do I fire a Client event from the COM Server( Inside the Thread Function)
Posted by mamthababu
on 02/08/2005 12:24pm
I am newbie to ATL/COM. I really appreciate your work(particularly your technical article on COM Connection Points).
May I know how do I create a thread in the COM Server (my own) method and then fire the Client event from the (newly Created) Thread's function?
When I create a Thread using the below code, it reports an error saying "global functions do not have 'this' pointers".
hThread = CreateThread( NULL, // default security attributes
0, // use default stack size
ThreadFunction, // thread function
reinterpret_cast< void* >(this), // argument to thread function
0, // use default creation flags
&dwThreadId); // returns the thread identifier
if (hThread == NULL)
DWORD dw = GetLastError();
sprintf(szBuf, "CreateThread Function Failed - GetLastError returned %u\n",dw);
MessageBox(NULL, szBuf, "Error", MB_OK);
Or say for example, in your article(COM Connection Points), how do I call the add(number1, number2)inside a Thread.
Any help would be greately appreciated.
Many thanks in advance,
Yes, I had a similar problem when using VB for the client part. It seems that events get lost while the VB window doesn't have the focus. This happened quite often. So I decided to have a mutex protected FIFO queue for results reading all pending msgs upon any event received as well as on timer events.
After all: I still prefer a TCP/IP stream or a Named Pipe!
Excellent tutorial, thanks! My only minor gripe would be that the client stuff isn't too easy to follow if you haven't used MFC before, I found I had to refer to the source files within the downloadable zip. As I say, that's only a very minor criticism though.