سؤال

I'm trying to use a bunch of webbrowsers in some background threads. This works no problem when I use webbrowser controls that i have placed on the form in design view but now when they are created at runtime.

I declare the webbrowsers array globally:

Dim webbroswers(-1) As WebBrowser

The following code is on the main thread:

ReDim Preserve webbroswers(somenum)

  For i = 0 To sumnum

            webbroswers(currentbrowsermax + i) = New WebBrowser

  Next

Then this code is run on the background thread:

If webbroswers(num).InvokeRequired Then
    webbroswers(num).Invoke(Sub() webbroswers(num).Navigate(someurl))
Else
    webbroswers(num).Invoke(Sub() webbroswers(num).Navigate(someurl))

The program crashes at this point with the following error:

Unable to get the window handle for the 'WebBrowser' control. Windowless ActiveX controls are not supported.

Any help on this would be great. Also if anyone knows how to suppress script errors then I think this might help. I've tried: WebBrowser(num).ScriptErrorsSuppressed = True but this doesn't work (it doesn't work elsewhere in my code when running on the main thread either) Thanks!

هل كانت مفيدة؟

المحلول

The Control.InvokeRequired and Invoke members use the Handle property to figure out what thread owns the control. Problem is, the Handle is null for the web browsers that you created. A control only has a valid handle when you made it visible on a form. Which you didn't do. It will then try to create the handle but that's a fail whale, an ActiveX control like WebBrowser needs a valid Parent. Without Me.Control.Add(), as was done in your original version, it won't have one.

The workaround is simple, you just need another control with a valid Handle property. Any will do, it only cares about the thread that owns the handle, not the value of the handle.

You have one: your form. Use Me.InvokeRequired and Me.Invoke() instead. Or Application.OpenForms(0) if you can't easily get a reference to the form object, best avoided.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top