Results 1 to 9 of 9

Thread: [RESOLVED] Cannot use Webbrowser control

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Resolved [RESOLVED] Cannot use Webbrowser control

    I have a program that uses backgroundworkers to load database data. I also added a webbrowser control to my form. The web page loads via the on load event. There is a button which calls a the background worker dowork event. This causes an error although my webbrowser control is not anywhere in the dowork code block. Anyone else experience this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Cannot use Webbrowser control

    If only there was some way for us to know more than just "an error" had occurred. If only there was someone who could show us the code that was being executed, tell us the error message and show us the line it occurred on. If there was some way for us to get that information then we might be able to work out what is causing the issue.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Re: Cannot use Webbrowser control

    I apologize. I am so terrible at supplying information. The error im getting is


    An error occurred creating the form. See Exception.InnerException for details. The error is: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Cannot use Webbrowser control

    I'm able to use both the AxWebBrowser and WebBrowser controls on the same form as a running BackGroundWorker so the problem must be in your code somewhere. Is all your code in the one form? Are you adding the browser and BackgoundWorker at design time?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Re: Cannot use Webbrowser control

    I can also run the background worker in the same form. But when i try to open a second form that has a background worker it causes this error. The second form does not contain a webbrowser. The code I use to open a new browser is.

    Code:
     If dgvProjectList.SelectedRows.Count > 0 Then
                For i = 0 To dgvProjectList.SelectedRows.Count - 1
                    Dim ProjectForm As New Project
                    ProjectForm.ProjectNumber = dgvProjectList.SelectedCells.Item(0).Value
                    ProjectForm.Show()
                Next
            End If
    the code is executed in a doubleclick event of the Datagriview.

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Cannot use Webbrowser control

    Well I've now tried several different combinations of multiple browsers, multiple background workers, multiple default forms and multiple generated forms and I've yet to meet a problem so I'm really not sure what to suggest.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    40

    Re: Cannot use Webbrowser control

    I figured it out. I have a mainform that contains a webbrowser control. It also contains a function called openconnection which opens a connection to mysql database. When the second form opens it calls the dowork function of the backgroundworker which then calls the openconnection function from the mainform. I guess the webbrowser control somehow ties itself to the main thread or creates its own thread? Idk but I fixed it by creating the openconnection function in the second form instead of calling it from the mainform which worked super well. Also I apologize for bad english as this is my second language.

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Cannot use Webbrowser control

    Also I apologize for bad english as this is my second language.
    If you hadn't said anything I really wouldn't have noticed! Glad you got it sorted out.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: [RESOLVED] Cannot use Webbrowser control

    The issue is that you were trying to create a ActiveX web browser control instance on a thread whose apartment state was multi-threaded. Note that the .NET WebBrowser control is really just a managed wrapper for that ActiveX control, so you're using the ActiveX control either way. The main thread of your app is single-threaded apartment state so create such a control on that thread is OK. If you were to create a Thread explicitly you could set its apartment state to single-thread. All thread pool threads are multi-threaded apartment state though, so any mechanism that uses the ThreadPool, e.g. a BackgroundWorker, will be unable to create such a control in a method that is executed on a secondary thread.

    Having said all that, while there may be reason to on occasion, you should generally avoid creating forms on any thread other than the main application thread. It's not called the UI thread for nothing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width