I encounter this problem when return HtmlDocument as property from my custom user control. (Which embedded WebBrowser control)
Cause of error because access document from other thread.
/// <summary>
/// Error version '
/// </summary>
public HtmlDocument Document
{
get
{
// Throw error 'Specified cast is not valid'
return this.webBrowserMain.Document;
}
}
But I don't known why error is not 'CrossThread Operation access ...' but next code solved my problem
/// <summary>
/// Fixed version
/// </summary>
delegate HtmlDocument DlgGetDocumentFunc();
public HtmlDocument GetDocument()
{
if(InvokeRequired)
{
return (HtmlDocument)this.webBrowserMain.Invoke(new DlgGetDocumentFunc(GetDocument), new object[] { });
}
else
{
return this.webBrowserMain.Document;
}
}