Click here to Skip to main content
15,900,707 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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):
C#
private System.Windows.Forms.WebBrowser m_web_browser;

And here is the relevant code from the function that handling this scan:
C#
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;

C#
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:
C#
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:
C#
try
{
    m_web_browser.Navigate(url);
}
catch (Exception sysEx)
{
    System.Diagnostics.EventLog.WriteEntry("xxx", sysEx.ToString());         
}

this is what i do:
C#
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)
{
    // Here I use the downloaded page content and reset the datamember flag
    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
Posted
Updated 7-Jan-12 0:08am
v5

1 solution

 
Share this answer
 
Comments
Eranz1111 11-Jan-12 7:27am    
Hi,

My code is running under a windows service, and under a web site page - both are already defined as STA (it didn't work at all until I changed the thread definition to STA). But it does not solve the problem I described above that occures only sometimes.
I now know for sure that this line:
this.m_web_browser.ScriptErrorsSuppressed = true;

also fails sometimes (until restarting the windows service/app pool), with this error:
[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,
Eran.

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