CWnd::RunModalLoop in C#

G

Girish

I was wondering if anybody has the implementation of CWnd::RunModalLoop/
CWnd::EndModalLoop in C#

Mine is a single threaded application... what I want is, the application
to wait at one point (and continue processing the messages) until some
event sets a flag, which will let the waiting code to continue...

e.g.

someFun()
{
...
...
... (some code which will fire someEvent later)
flag = waiting
Wait and process other messages
if flag == continue process stmts bellow else continue waiting
(Above 3 stmts are processed in CWnd::RunModalLoop)
...
...
}

someEvent()
{
...
...
flag = continue
(Above stmt is processed in CWnd::EndModalLoop)
}

I tried implementing these functions without any luck, can somebody tell
me what am I doing wrong...

-------------------------------------------------------
public static int RunModalLoop()
{
m_nFlags |= (WF_MODALLOOP|WF_CONTINUEMODAL);
for(;;)
{
MSG msg = new MSG();

GetMessageA(ref msg, 0, 0, 0);
do
{
TranslateMessage(ref msg);
DispatchMessageA(ref msg);
if (!ContinueModal())
goto ExitModal;
}while(PeekMessageA(ref msg, 0, 0, 0, PM_NOREMOVE) != 0);
}
ExitModal:
m_nFlags &= ~(WF_MODALLOOP|WF_CONTINUEMODAL);
return m_nModalResult;
}

private static bool ContinueModal()
{
return (m_nFlags & WF_CONTINUEMODAL) == WF_CONTINUEMODAL;
}

public static void EndModalLoop(int nResult)
{
m_nModalResult = nResult;

// make sure a message goes through to exit the modal loop
if (ContinueModal())
{
MSG msg = new MSG();
msg.hwnd = IntPtr.Zero;
msg.lParam = 0;
msg.message = 0;
msg.time = 0;
msg.wParam = 0;

m_nFlags &= ~WF_CONTINUEMODAL;
PostMessageA(0, msg, 0, 0);
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

Girish,

I really don't think that this is a good idea. I don't believe that
handling the message loop on your own is a good idea.

Rather, I would create a new thread, and have that thread wait. When
the event is fired, you call Invoke on a control made in the UI thread,
which will continue the processing once the event is complete.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top