• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1358
  • Last Modified:

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;
}
0
FALTSKOG
Asked:
FALTSKOG
1 Solution
 
FALTSKOGAuthor Commented:
All I want to know is how to handle event notifications the MFC way! :)
0
 
jhanceCommented:
I'd suggest the CEvent MFC class.
0
 
elcapitanCommented:
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--
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ambienceCommented:
do you want to know how to handle callbacks in MFC/classes or just curious about Events ?
0
 
FALTSKOGAuthor Commented:
Ok - I know how to create events using CEvent, but where is the callback?
0
 
FALTSKOGAuthor Commented:
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)?
0
 
ambienceCommented:
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.
0
 
ambienceCommented:
>> 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 .
0
 
FALTSKOGAuthor Commented:
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
0
 
ambienceCommented:
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 ..
0
 
FALTSKOGAuthor Commented:
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.
0
 
ambienceCommented:
it has to be in a loop , well you can have that effect of listening and event handler , but then you have to implement an EventHandler yourself which ofcourse would go on in a loop keep looking for events and when it finds one it does a callback or posts a win32 message whatever way you like it , but yes you cannot avoid the loop anyway..

atleast that is what i know maybe someone else has brighter ideas
0
 
FALTSKOGAuthor Commented:
I think I've got what I need. Thanks for your help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now