Solved

Synchronization between instances of same processes using CreateEvent

Posted on 2014-03-19
1
275 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
1 Comment
 
LVL 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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…
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…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

19 Experts available now in Live!

Get 1:1 Help Now