Solved

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

Posted on 2004-09-21
3
212 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
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
jlsjls earned 500 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now