I wanted to know why CreateEvent was used and what is the Necessity of using that in my VC++ application

  • Thread starter Thread starter aditya satya prakash
  • Start date Start date
A

aditya satya prakash

Guest
Hello all,

In my project CreateThread() function was used to create a worker thread. And then in the Worker thread function ::CreateEvent was used.

I know that the CreateEvent function is used to create the event thread synchronization object. And also we can use event objects when a thread is supposed to start doing its job after a specified action has occurred.

But here in the current scenario, I wanted to know why ::CreateEvent() function was used and what is the Necessity of using that?

Could any one please provide your thoughts on this?

And also in the below example, we are taking the handle of the ::CreateEvent() and if it is not nulll we are executing the rest of the code.
And then we are assigning this handle to the newly created HANDLE to a pointer object (*pAllHandles).

Why we are doing likes this?

Below is the code snippet:

//Create the worker thread
g_hThread = CreateThread(NULL, 0,(LPTHREAD_START_ROUTINE) &serviceworker_thread, NULL, 0, &dwThreadID);


// Worker Thread function

DWORD WINAPI serviceworker_thread (LPVOID pThreadParameter)
{
DWORD errStatus = ERROR_SUCCESS;


HANDLE hStopApplicationEvent = ::CreateEvent(0, TRUE, FALSE, APC_STOP_APPLICATION_EVENT_NAME);
if (hStopApplicationEvent)
{

HANDLE * pAllHandles = new HANDLE;

assert(pAllHandles != NULL);

if (pAllHandles)
{
*pAllHandles = hStopApplicationEvent;

CDaliPowerHandler powerHandler(FIREFLY_SERVICE_WINDOW_CLASS_NAME, FIREFLY_SERVICE_WINDOW_NAME);

HWND hWnd = powerHandler.GetHwnd();

SetTimer(hWnd, APC_DEFAULT_INTERVAL, 5000, NULL);
SetTimer(hWnd, APC_HEARTBEAT_INTERVAL, 3600000, NULL);


if (hWnd)
{
WaitForMessagesAndEvents(1, pAllHandles, 0, hWnd);
}
else
{
assert("worker_thread()" == NULL);
}
}
delete [] pAllHandles;
}
else
{
assert("worker_thread()" == NULL);
}

if (hStopApplicationEvent)
{
CloseHandle(hStopApplicationEvent);
}

if (hStopLoggingEvent)
{
CloseHandle(hStopLoggingEvent);
}

return(errStatus);
}

And also in WaitForMessagesAndEvents() function, MsgWaitForMultipleObjects is used like as shown below:

int WaitForMessagesAndEvents (int aTotalNumHandles, HANDLE * pHandlesArray,int aNumTasks, HWND ahWnd)
{

if (ahWnd)
{
::PeekMessage(&msg, ahWnd, WM_USER, WM_USER, PM_NOREMOVE);

while ((bQuitLoop == FALSE) && (bEventFlag == FALSE))
{
res = MsgWaitForMultipleObjects(aTotalNumHandles, pHandlesArray, FALSE, INFINITE, QS_ALLINPUT);

if (res == WAIT_OBJECT_0 + aTotalNumHandles)
{
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
// if (msg.message == WM_QUIT)
if (msg.message == WM_DESTROY)
{
bQuitLoop = TRUE;
exitCode = msg.wParam;
//StopAllLaunchedTasks(aNumTasks, pTaskLoaderArray);

if (bAdvancedUPS)
{
InitializeShutdownSetting(TRUE);
///Check to see if we need to disable the alarm on shutdown.
TurnOffAlarmsIfUserSettingTrue();
}
pfc_unregister_9X_service();
break;
}
}
}
}
}




Y A S Prakash

Continue reading...
 
Back
Top