Solved

CreateEvent - how to do it in MFC

Posted on 2001-06-25
13
1,281 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now