Hello,
I have a strage and inconsistent problem.
I am using System.Windows.Forms.WebBrowser object in order to scan web pages content from some external web pages (in that way I am able to get the full content that I need to use).
I use it under 2 processes running on the same machine: IIS7 and a windows service (both calls the same function that uses it).
Here is its definition (a data member of my class):
private System.Windows.Forms.WebBrowser m_web_browser;
And here is the relevant code from the function that handling this scan:
this.m_web_browser = new System.Windows.Forms.WebBrowser();
this.m_web_browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.m_web_browser_DocumentCompleted);
this.m_web_browser.ScriptErrorsSuppressed = true;
try
{
m_web_browser.Navigate(url);
}
catch (Exception sysEx)
{
System.Diagnostics.EventLog.WriteEntry("xxx", sysEx.ToString());
}
And the problem is:
Most of the time, it works fine. Until I start getting this exception with this command:
m_web_browser.Navigate(url);
System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.
at System.Windows.Forms.UnsafeNativeMethods.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
at System.Windows.Forms.WebBrowser.PerformNavigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData, Object& headers)
at System.Windows.Forms.WebBrowser.Navigate(String urlString)
When this exception starts to occur, it will raise every time until I reset the Application pool or the windows service, and then it works fine again (until it will start happen again...).
Some more information:
At the start of this function I set up another data member of the class to false:
download_complete_ind = false;
At the m_web_browser_DocumentCompleted method I set it to true.
download_complete_ind = true;
After the scope I mentioned above:
try
{
m_web_browser.Navigate(url);
}
catch (Exception sysEx)
{
System.Diagnostics.EventLog.WriteEntry("xxx", sysEx.ToString());
}
this is what i do:
Application.DoEvents();
int sleepsNum = 0;
Int16 sleepsMaxNum = 20;
int sleepMilliseconds = 500;
while (!download_complete_ind && sleepsNum < sleepsMaxNum)
{
System.Threading.Thread.Sleep(sleepMilliseconds);
sleepsNum++;
Application.DoEvents();
}
if (download_complete_ind)
{
m_mirror_site_download_complete_ind = false;
}
else
{
throw new Exception("content wasn't found");
}
m_web_browser = null;
-----------------------------------------------
Another issue - there are times that I have another exception which is also solved only after restart:
According to the exception details, it seems that this is the command that fails:
this.m_web_browser.ScriptErrorsSuppressed = true;
I now wrapped this line with a try-catch scope to be sure. Meanwhile, here is the exception details:
System.Reflection.TargetInvocationException: Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported. ---> System.ComponentModel.Win32Exception: Error creating window handle.
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.WebBrowserBase.DoVerb(Int32 verb)
at System.Windows.Forms.WebBrowserBase.TransitionFromRunningToInPlaceActive()
--- End of inner exception stack trace ---
at System.Windows.Forms.WebBrowserBase.TransitionFromRunningToInPlaceActive()
at System.Windows.Forms.WebBrowserBase.TransitionUpTo(AXState state)
at System.Windows.Forms.WebBrowser.get_AxIWebBrowser2()
at System.Windows.Forms.WebBrowser.set_ScriptErrorsSuppressed(Boolean value)
Thanks!
EZ