Solved

Synchronization between instances of same processes using CreateEvent

Posted on 2014-03-19
1
320 Views
Last Modified: 2014-03-27
Hello experts

I have a problem when synchronizing the same instances of a process. My application will call from other jobs simultaneously , it instantiate the multiple instances of the application.

 I want to create a event in the application and signal it  after completion of one instance I will un-signal it,  other instances should instances should wait until first completes.

created sample code. but it is in infinite loop.

            HANDLE  hEvent1 = CreateEvent ( NULL , true , false , L"MyEvent" );
            hEvent = OpenEvent ( EVENT_ALL_ACCESS , false, L"MyEvent" );
            if(GetLastError () == ERROR_ALREADY_EXISTS)
            {
                  while (WaitForSingleObject(hEvent,INFINITE)==WAIT_OBJECT_0)
                  {
                        Sleep(0);
                  }
                  SetEvent (hEvent);
            }
            else
                  SetEvent (hEvent );




//in the ending of application.
ResetEvent (hEvent);


Please advise
Thanks in advance
0
Comment
Question by:HarishMinu
[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
1 Comment
 
LVL 34

Accepted Solution

by:
sarabande earned 500 total points
ID: 39944788
HANDLE  hEvent1 = CreateEvent ( NULL , true , false , L"MyEvent" );
 hEvent = OpenEvent ( EVENT_ALL_ACCESS , false, L"MyEvent" );

I would assume that the second call would return with success but has the GetLastError as ERROR_ALREADY_EXIST. then you wait infinite on the event created by the instance itself.

you would either need to pass the EVENT_ALL_ACCESS with the CreateEvent and omit the OpenEvent call (note the CreateEvent would also work for an already created event and would return a valid handle).  or you would check the GetLastError before calling OpenEvent. I would prefer the first alternative.

Sara
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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