Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi all,
I'm using WTL and COM, I tried to implement refreshing my webpage after reconnect to the internet. I have a thread monitering the net status who will call SendMessage to notify main thread to reload the webpage using Navigate2.

Unfortunately,I ran into an odd occurrence.After the main thread receive the notification and call navigate,I found documentcomplete event not fired.But after replacing SendMessage with PostMessage, everything works fine.why would this happen?

C++
void CFrmWeb::OnNetStateChanged(bool good)
{
    if(good && !m_bIsNetAvailable)
    {
        if(::IsWindow(m_peerEntity.m_hWnd))
        {
            ::SendMessage(GetHandle(),WM_NETWORK_RECOVERY,0,0);
        }
    }
    m_bIsNetAvailable = good;
}


C++
BOOL CFrmWeb::Fx_ProcessWindowMessage(CSHSignalArgs &e)
{
	UINT msg = e.m_msg.message;
	if(msg == WM_NETWORK_RECOVERY)
	{
	     ShowPage(m_strUrl);
	}
	return __super::Fx_ProcessWindowMessage(e);
}


ShowPage is a wrapper for Navigate2 of WebBrowser2,Fx_ProcessWindowMessage is the message handler.I have confirmed that the WM_NETWORK_RECOVERY is received and showpage is called in main thread,but I just can't get documentcomplete fired. But when using PostMessage, everything works fine!

Please help me out, any explanation would be appreciated!
Posted
Updated 26-Aug-13 18:56pm
v2
Comments
Sergey Alexandrovich Kryukov 26-Aug-13 23:50pm    
Not clear. Without having some short but complete code sample, it's hard to say what you could screw up.
—SA
Sergey Alexandrovich Kryukov 27-Aug-13 0:04am    
Your formatting won't work in a comment. Instead, click here: improve question, to move your code to your question. Besides, add the attribute language="C++" to your "pre" tag.

And I'm still not sure this sample will show the problem...
—SA
findewayz 27-Aug-13 1:02am    
Thanks for your suggestion.I just updated the question. Basically what I mean is that documentcomplete event is not fired on main thread after I use SendMessage to notify main thread to navigate to an url address. But using PostMessage instead of SendMessage can solve the problem. I just wanna know the reason!
Sergey Alexandrovich Kryukov 27-Aug-13 1:23am    
Do you mean that navigation is done correctly in both cases, only the event is not invokes with SendMessage?
Or navigation is not successful with SendMessage?

SendMessage is incorrect because you need a main message loop to process all messages, but your code would be blocking it until SendMessage returns you a message result value. PostMessage does not create this problem.

—SA
findewayz 27-Aug-13 1:53am    
Navigation is done correctly in both cases. You said that SendMessage would block my code execution. But I use SendMessage in a new thread,it will only block that thread that called it not the main thread,right? DocumentComplete is expected to be received in main thread. I don't get it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900