?
Solved

Events from OCX do not fire when the client app is busy with another event. [Events dropped]

Posted on 2004-09-21
3
Medium Priority
?
218 Views
Last Modified: 2012-06-27
If my OCX tries to fire an event but the client app is busy with another event, the event will be lost.
I am writing the OCX in VC++ 6, this OCX will be loaded multiple times in the client app.

Here is an example of my problem.

I have 2 instances of the OCX loaded on the client app.
OCX1 fires an event -> the client app receives the event and starts processing the code in the event.
If OCX2 fires an event while the client is still processing the previous event, this event will be lost.

Another example to help visualize the problem:

OCX1 fires an event
Client runs slow code in the event such as Sleep(1000)
OCX2 fires an event before the OCX1 event finishes
Client does not receive OCX2 event

My current solution that works but i cannot use because it uses ties up the CPU.
I have a global variable called HoldEvents [all instances of the OCX can read/set this variable]
my code to fire the event now looks as follows

while(HoldEvents) {}
HoldEvents=1;
FireBlahEvent(blah);
HoldEvents=0;

Does anyone know of a better solution? I don't like the idea of a loop and i cannot add sleep in the loop to use less CPU because the events are time critical and need to be fired as soon as the client app can take them.

Joe.



0
Comment
Question by:joevm3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
jlsjls earned 2000 total points
ID: 12130922
I think the use of a Mutex could be a solution.
When you fire the event you try to get the ownership of the mutex (WaitForSingleObject(hMutex,INFINITE).
When OK, you fire the event to the application.
The application processes the event and after returning to the ocx you release the mutex.

At that moment, ocx2 can take the ownership of the mutex and can fire its data to the application. Again using procedure mentioned above.
Waiting for the mutex doesn't practically stress the CPU.

jlsjls
0
 

Author Comment

by:joevm3
ID: 12147231
how do you release the mutex?
what is hMutex... how do you initalize the mutex?

Thank you,
Joe
0
 
LVL 3

Expert Comment

by:jlsjls
ID: 12157998
* Creation of mutex (obtain handle hMutex to it) :
BOOL MakeMutex()
{
hMutex = CreateMutex(NULL,FALSE,TEXT("MyMutex"));
if (hMutex == NULL)
{
 return FALSE;
}
else
{
if (ERROR_ALREADY_EXISTS == GetLastError())
//mutex already created by other process or other thread
{
//MessageBox (NULL, TEXT("CreateMutex opened existing mutex."),TEXT("Results"), MB_OK);
}
else //new mutex was created
{                   
//MessageBox (NULL, TEXT("CreateMutex created new mutex."),TEXT("Results"), MB_OK);
}
}
return TRUE;
}

* Release the mutex
ReleaseMutex(hMutex);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question