1

I'm trying to write a simple C++ app that registers to Windows sensor events. I followed the MSDN documentaion and managed succesfully to get notifications when sensor events occur, my problem is that my main function ends, and so does the application. How to i cuase it to wait forever for events to occur? Currently it registers and dies...

I have the following code:

My main looks like this:

int _tmain(int argc, _TCHAR* argv[])
{
   RegisterToSensorEvents();
   return 0;
}

void RegisterToSensorEvents()
{
    ISensorManager* pSensorManager = NULL;
    CoInitialize(NULL);
    HRESULT hr = ::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pSensorManager));

    // Get a collection of all sensors on the computer.
    ISensorCollection* pSensorCollection = NULL;
    hr = pSensorManager->GetSensorsByCategory(SENSOR_CATEGORY_ALL, &pSensorCollection);

    EventsManager *pEventClass = NULL;
    ISensorEvents* pMyEvents = NULL;
    pEventClass = new(std::nothrow) EventsManager();   
    hr = pEventClass->QueryInterface(IID_PPV_ARGS(&pMyEvents));

    ULONG numOfSensors;
    pSensorCollection->GetCount(&numOfSensors);
    for(int i=0; i< numOfSensors; i++)
    {
        ISensor *sensor = NULL;
        pSensorCollection->GetAt(i,&sensor);
        hr = sensor->SetEventSink(pMyEvents);
    }
}

EventsManager is a class that derives from ISensorEvents and implements its callbacks, for example:

STDMETHODIMP EventsManager::OnDataUpdated(ISensor *pSensor,ISensorDataReport *pNewData)
{
    cout  <<"got here: Data Update" << endl;
}

I tried:

 int _tmain(int argc, _TCHAR* argv[])
 {
   RegisterToSensorEvents();
   while(true){}
   return 0;
 }

but seems like this infinte loop did not leave time for the program to process the incomming events, I tried adding Sleep in the loop body, but it didn't work either.

anyone?

UPDATE:

after investigation i see that the issue is different - seems like somehow my registartion of SetEventSink gets canceled and that is why i don't get any event notification.

if i copy this line: hr = sensor->SetEventSink(pMyEvents); into my loop:

while(true)
{
   hr = sensor->SetEventSink(pMyEvents);
}

the events are fired as expected. But it sounds to me very wrong to do such a thing.

Need to understand why this is hapenning.

Can anyone help?

one
  • 511
  • 5
  • 17
  • 2
    You're forgetting the [Windows message event loop](https://en.wikipedia.org/wiki/Message_loop_in_Microsoft_Windows). – Some programmer dude Feb 29 '16 at 06:53
  • can you provide the exact code cause infinite loop to not giving proper time to your code to execute something? Because infinite loop is the solution for not ending program. – AchmadJP Feb 29 '16 at 07:30
  • How do you actually use the Windows message event loop? – one Feb 29 '16 at 09:54
  • Is RegisterToSensorEvents a well known function of some library (I don't find it on Google) ? Otherwise you should provide more info on it. Is it starting a thread or something ? Otherwise, here, your infinite loop is not doing anything, not even reading events so there's no chance your program can do anything ! – Colin Pitrat Feb 29 '16 at 09:56
  • added the code of my RegisterToSensorEvents function, the problem is not in code since i see it gets invoked if i debug with breakpoints, i'm just looking for something i need to add to my main so it won't terminate – one Feb 29 '16 at 10:09
  • 1
    @AchmadJP: no, it really isn't - or at least not a good one. Why waste all those CPU cycles? If you really want to stop dead and not do anything, you should use `Sleep(INFINITE)` instead. See also http://stackoverflow.com/questions/3592557/optimizing-away-a-while1-in-c0x – Harry Johnston Feb 29 '16 at 20:58
  • As Joachim said, **you need a message loop.** COM won't work without one. I think a call to `MessageBox` would be sufficient. – Harry Johnston Feb 29 '16 at 21:00

1 Answers1

1

Why don't you launch a new thread to do the listening, and just have the main function wait for an input?

How to simulate "Press any key to continue?"

Simple example of threading in C++

You can combine these to get your desired result.

Community
  • 1
  • 1
Carlos
  • 5,991
  • 6
  • 43
  • 82
  • my main now looks like this: int _tmain(int argc, _TCHAR* argv[]) { std::thread registerToSensor(RegisterToSensor); registerToSensor.join(); system("pause"); return 0; } and it is still not working - the event gets fired a few times and that's it, once my program reaches the pause, events stop – one Feb 29 '16 at 11:02
  • Does it actually exit? – Carlos Feb 29 '16 at 11:35
  • no, it just wait there for an input, but events are not fired – one Feb 29 '16 at 19:29
  • Where does it actually hook the event listener? – Carlos Feb 29 '16 at 19:38