Link to home
Start Free TrialLog in
Avatar of FALTSKOG
FALTSKOG

asked on

CreateEvent - how to do it in MFC

I've got a piece of code that uses CreateEvent and shows me how to receive notifications via the callback. How can I implement this with the C++/MFC methodology?

the callback is like this...

DWORD WINAPI NotificationProc( LPVOID lpParameter )
{
    HRESULT hr;
    HWND    hDlg = (HWND) lpParameter;
    MSG     msg;
    DWORD   dwResult;
    BOOL    bDone = FALSE;
    BOOL    bLooped;

    while( !bDone )
    {
        dwResult = MsgWaitForMultipleObjects( 1, &g_hNotificationEvent,
                                              FALSE, INFINITE, QS_ALLEVENTS );
        switch( dwResult )
        {
            case WAIT_OBJECT_0 + 0:
                // g_hNotificationEvent is signaled

                // This means that DirectSound just finished playing
                // a piece of the buffer, so we need to fill the circular
                // buffer with new sound from the wav file
                bLooped = ( IsDlgButtonChecked( hDlg, IDC_LOOP_CHECK ) == BST_CHECKED );
                if( FAILED( hr = g_pStreamingSound->HandleWaveStreamNotification( bLooped ) ) )
                {
                    DXTRACE_ERR( TEXT("HandleWaveStreamNotification"), hr );
                    MessageBox( hDlg, "Error handling DirectSound notifications."
                               "Sample will now exit.", "DirectSound Sample",
                               MB_OK | MB_ICONERROR );
                    bDone = TRUE;
                }

                break;

            case WAIT_OBJECT_0 + 1:
                // Messages are available
                while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
                {
                    if( msg.message == WM_QUIT )
                        bDone = TRUE;
                }
                break;
        }
    }

    return 0;
}
Avatar of FALTSKOG
FALTSKOG

ASKER

All I want to know is how to handle event notifications the MFC way! :)
I'd suggest the CEvent MFC class.
Like jhance suggested, you can use the CEent class that wraps the event API, or you can use the windows event APIdirectly. The CEvent has the following member methods:
SetEvent() - Sets the event to available (signaled) and releases any waiting threads.
PulseEvent() - Sets the event to available (signaled), releases waiting threads, and sets the event to unavailable (nonsignaled).
ResetEvent() - Sets the event to unavailable (nonsignaled).
Unlock() - Releases the event object.

--EC--
Avatar of ambience
do you want to know how to handle callbacks in MFC/classes or just curious about Events ?
Ok - I know how to create events using CEvent, but where is the callback?
The example I give at the top just implements an event message loop that is started as a new thread. This is the example as given for a Win32 app - not MFC. Where is this loop in MFC and how do I receive the event notifications - or do I still have to implement this event loop thread as above? If I do start this event loop, will it affect normal windows events (I noticed it checks for a WM_QUIT)?
This we cannot easily tell unless you post some code, or tell us what is the nature of callbacks , from this all i can say is that callback functions are supposed to be static members of the class that you are using , and you pass the pointer to the object as the parameter to the callback function for example

DWORD CMyWnd::NotifyProc(LPVOID lpv)
{
   CMyWnd * pWnd = (CMyWnd*)lpv;
   HWND = pWnd->GetSafeHwnd();
   ....
  ....
}

and when you call it instead of passing a HWND you pass pointer to a CMyWnd and if you are calling it from inside CMyWnd just pass this pointer.
>> Where is this loop in MFC ??
Hidden inside the bowels of CWinThread::Run()

>> I still have to implement this event loop thread as above? If I do start this event loop, will it affect normal windows events (I noticed it checks for a WM_QUIT)?

Sure it will , since CWinThread::Run does some other stuff too in addition to Translate and DispatchMessage, for e.g. it calls OnIdle() and also pretranslatemessage , so either you are willing to do that youself (not a good idea).

The way i see is by implementing a separate thread for it. The thread does nothing but keep looking for events and when it finds one it posts it as a message to the main window , where you have normal message handlers for that message.

but there are other variations too .
I'm not explaining this well.
I create an event with CreateEvent and the handle is global. The threaded function (NotificationProc) then just waits for my events using MsgWaitForMultipleObjects and calls functions that do whatever - great!

I'm using this in conjunction with DirectSound - DirectSound gets passed a list of event handles along with details about which time to signal them. I then pass this list/array of events to the MsgWaitForMultipleObjects. Direct sound monitors it's list of sync points and flags the associated event when appropriate. I then know this has happened as my NotificationProc is looking for all the registered events.

So how is this done in MFC - it may not be possible, which is an acceptable answer!

If I create CEvents I still need to pass an array of handles to the DirectSound event register - it won't take a CEvent. Can you get an equivalent handle that CreateEvent offers with CEvent?

Assuming I can register the CEvents, how do I wait for the events to occur? Is there a class that implements an event listener? If there is, could I pass a list of CEvents that it waits for - like MsgWaitForMultipleObjects but with CEvents rather than handles?

I hope this makes it clearer
1) CEvent has operator HANDLE defined so it is usable inplace of a normal HANDLE.

2) There is a class CMultiLock that would implement waiting on mulitple CEvents , check it out ..
So the lock waits for the events. Do I still have to create a loop and repeatedly call the lock to wait repeatedly? I guess this loop would have to be in a separate thread too? I was hoping I could listen permanently by just adding an event handler - like handling the WM_TIMER event.
ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think I've got what I need. Thanks for your help.