-1

How would I setup a custom event and event handler in Windows C++?

An analog for what I want to accomplish would be something similar to what this is doing in nodejs:

//in main execution context:

var app = require('myapp');

app.on('ready', function(){
    //An event handler for the ready event
})

var i = 10;
foo(i);
//... do other stuff 

================================================
//in a different execution context:

app.doStuff();
app.emit("ready");

What is the Windows C++ mechanism for setting and event handler and continuing execution?

1
  • You'll need to clarify what exactly you mean by "Windows C++", raw winapi? MFC? Commented Apr 7, 2016 at 1:26

2 Answers 2

1

Event handler is an abstract concept that exists somehow is all general purpose programming languages.

C++ is no exception. For C++ under windows you can define a custom event, a source that will raise this event, and a receiver or an event listener/handler.

Check the sample code here

https://learn.microsoft.com/en-us/cpp/cpp/event-handling-in-native-cpp

Sign up to request clarification or add additional context in comments.

2 Comments

Huh, alright, I guess I was expecting something built into C++ for custom events. Thanks!
0

Here the way you can handle event in C++.

            CMyApp::CMyApp()
            {
             m_hThreadEvent = 0;
            }

            // strcuture for send parameter to ThreadProc
            struct sThreadProcParam
            {
               HANDLE m_hEvent; //Handle close the app event.
            }

            UINT CMyApp::InitializeApp()
            {
               sThreadProcParam *param = new sThreadProcParam;
               param->m_hEvent = 0;
               
               m_hThreadEvent = CreateEvent(NULL, FALSE , FALSE ,NULL);
               if(!m_hThreadEvent) return 1;
               
               param->m_hEvent = m_hThreadEvent;
               
               HANDLE Hwnd = CreateThread(NULL,0, ThreadProc , param , 0 , NULL)
               if(!Hwnd) return 1;
               
               //Rest of code for initialization
            }   

            CMyApp::OnClose()
            {
                //Signal to Thread function event
                SetEvent(m_hThreadEvent);
                m_hThreadEvent = 0;
                
                //Other code for close handle
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.