Solved

CreateEvent - how to do it in MFC

Posted on 2001-06-25
13
1,298 Views
Last Modified: 2013-11-20
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
Comment
Question by:FALTSKOG
13 Comments
 

Author Comment

by:FALTSKOG
ID: 6224959
All I want to know is how to handle event notifications the MFC way! :)
0
 
LVL 32

Expert Comment

by:jhance
ID: 6224967
I'd suggest the CEvent MFC class.
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 6226522
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 22

Expert Comment

by:ambience
ID: 6226761
do you want to know how to handle callbacks in MFC/classes or just curious about Events ?
0
 

Author Comment

by:FALTSKOG
ID: 6226821
Ok - I know how to create events using CEvent, but where is the callback?
0
 

Author Comment

by:FALTSKOG
ID: 6226845
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
 
LVL 22

Expert Comment

by:ambience
ID: 6226865
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
 
LVL 22

Expert Comment

by:ambience
ID: 6226914
>> 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
 

Author Comment

by:FALTSKOG
ID: 6226942
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
 
LVL 22

Expert Comment

by:ambience
ID: 6226957
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
 

Author Comment

by:FALTSKOG
ID: 6226998
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
 
LVL 22

Accepted Solution

by:
ambience earned 50 total points
ID: 6227011
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
 

Author Comment

by:FALTSKOG
ID: 6227055
I think I've got what I need. Thanks for your help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 53
Separate into files by filename 12 87
while loop over for loop 7 115
noX challenge 17 130
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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