I have a strange problem I cant seem to figure out. I have the following function for filling a webbrowser control in my desktop application with HTML. If I add a MessageBox then the html displays. If I remove the messagebox then I get a error that says "Object reference not set to an instance of an object". Does anyone have any ideas why this would happen?
private void fillPMMessage(string sHTML)
{
browserMessage.Navigate("about:blank");
try
{
IHTMLDocument2 doc = browserMessage.Document.DomDocument as IHTMLDocument2;
MessageBox.Show("Were here");
doc.body.innerHTML = sHTML;
}
catch(Exception exc)
{
MessageBox.Show("FillPMMessage:" + exc.Message.ToString());
}
}

1 answers
I would suppose that the browser needs time to prepare your document. Try to add System.Threading.Thread.Sleep(2000) (two seconds sleep) instead MessageBox.Show() so you can give the browser control enough time to its work.
From what I remember from my brief expecience with the WebBrowser control - I think it is asynchronous and it runs its stuff in another thread. So what I suppose happens in your case is:
Main thread sets the doc variable,
Another thread is started to get the document (meanwhile the doc variable is null)
You are presented a message box.
The helper thread fills up the doc variable.
Your code works.
Please note that this is only a guess. I have no idea what is going on ;)
Fingers crossed however.
answered 2 years ago by:
0
I would suppose that the browser needs time to prepare your document. Try to add System.Threading.Thread.Sleep(2000) (two seconds sleep) instead MessageBox.Show() so you can give the browser control enough time to do its work.
From what I remember from my brief expecience with the WebBrowser control - I think it is asynchronous and it runs its stuff in another thread. So what I suppose happens in your case is:
Main thread sets the doc variable,
Another thread is started to get the document (meanwhile the doc variable is null)
You are presented a message box.
The helper thread fills up the doc variable.
Your code works.
Please note that this is only a guess. I have no idea what is going on ;)
Fingers crossed however.
answered 2 years ago by:
0
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!