Solved

Synchronization between instances of same processes using CreateEvent

Posted on 2014-03-19
1
271 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 32

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

743 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

11 Experts available now in Live!

Get 1:1 Help Now