Solved

Synchronization between instances of same processes using CreateEvent

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

776 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