1

I am trying to create a rendering loop inside a C++/CLI library for a .NET Windows Form application but the PeekMessage never get any messages. So my render loop is infinite and the form appear frozen.

I have tried several way of doing it but here is my last try.

To start the rendering loop :

Application::Idle += gcnew EventHandler(this, &TECore::AppIdleHandler);

Code that handle it :

void TECore::AppIdleHandler(Object^ object, EventArgs^ e)
    {
        while (IsAppIdle())
            RenderLoopCallBack();
    }

    bool TECore::IsAppIdle()
    {
        LPMSG msg = {};
        return !PeekMessage(msg, (HWND)_targetForm->Handle.ToInt32(), 0, 0, 0);
    }

So if I am not doing anything wrong, I am constantly checking for message from my windows handle (also have tried with NULL) otherwise I do render stuff. But the window is frozen because I never get any message, IsAppIdle always return true. I can't focus the windows, resize it or anything..

Thank a lot for help.

EDIT 1 : It's working if I do Application::DoEvent() in each frame. But what's the performance drawback ?

EDIT 2 :

I am now pretty sure that my PeekMessage don't get any message because the RenderLoop is in the class library and not directly in the WindowForm. PeekMessage work fine directly in the form code. Is that a normal behaviors ? Maybe C# when loading a CLI assembly automaticly load it on another thread ? So my PeekMessage is looking on the wrong thread ?

Karnalta
  • 518
  • 1
  • 9
  • 24
  • I feel like my problem have to do with the fact that this loop is inside a class library. If I use it inside the Window Form it work fine. I probably doesn't really get how message stack is working :s – Karnalta Mar 30 '17 at 14:42

1 Answers1

0

I found my problem... Just a syntax error. Intellisense was asking for LPMSG as first parameter for PeekMessage (never used that before) and in fact I needed to pass a MSG pointer. So PeekMessage was probably failing in silence...

With the correct syntax it work fine.

WRONG SYNTAX

bool TECore::IsAppIdle()
{
    LPMSG msg = {};
    return !PeekMessage(msg, NULL, 0, 0, 0);
}

RIGHT SYNTAX

bool TECore::IsAppIdle()
{
    MSG msg = {};
    return !PeekMessage(&msg, NULL, 0, 0, 0);
}
Karnalta
  • 518
  • 1
  • 9
  • 24