locked
Web browser not responsive because it is in use...? RRS feed

  • General discussion

  • Hello,

    I added a web browser to my app but I am having trouble controlling it because every time I try to navigate the browser from another sub or function the app gets unresponsive and tells me that I can't control the browser because it is used by another procedure.

    I tried webbrowser.stop but that does not do anything. If I do webbrowser.dispose, I will have to create another web browser object and I don't know what will happen to windows.forms.webbrowser1 that I add to the form. Can I just keep making new web browser objects every time I want to navigate from a different sub or function?


    • Edited by Hani Deek Wednesday, November 30, 2011 7:47 AM
    Wednesday, November 30, 2011 7:45 AM

All replies

  • Hi,

    Please always post the EXACT error message as it easily avoids a possible confusion about what's going on.

    It seems to me you are trying to access the web browser control from another thread. All controls must be accessed from the UI thread.


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, November 30, 2011 10:19 AM
  • So what can I do?
    Wednesday, November 30, 2011 11:11 PM
  • How can I get back to the UI thread to access the controls without having to press a button or interrupt the code? I appreciate the help.
    Thursday, December 1, 2011 1:01 AM
  • I have done an experiment and I think that the problem is that I am using a System.Timers.Timer in my app. The code loses control of the interface tools right after I use the timer:

     

     

    Public Sub Sub1()
    
            Dim Timer1 As New System.Timers.Timer()
    
            Timer1.AutoReset = False
            Timer1.Interval = 5 * 1000
            Timer1.Enabled = True
    
            AddHandler Timer1.Elapsed, AddressOf Sub2
    
     End Sub
    
       
    
    Public Sub Sub2(ByVal source As Object, ByVal e As Timers.ElapsedEventArgs)
    
            Timer1.Dispose()
    
            etc.
    
    End sub

    I can access the controls normally in Sub1 (the sub which I finished with a timer), but in Sub2 (the sub which starts when the timer goes off) I lose control of the controls and never gain it back again unless I press a new button on form1.

    How can I avoid this problem? I need Sub2 to start few seconds after Sub1 finishes but I also want to keep control of the interface tools.

     

     

     



    • Edited by Hani Deek Thursday, December 1, 2011 2:28 AM
    Thursday, December 1, 2011 2:26 AM
  • Search for InvokeRequired, Invoke. It has been frequently debated. It allows to check if you are on the UI thread and if not to call the method on the UI thread.

    Depending on what you are doing it could be also a design issue (i.e. there is not point in doing things in a background thread if you always call the UI). Here Is suspect you have something wrong. The browser control is event based so I'm not sure why you need a background thread.

    Finally please try to respond to asked question. Not knowing the EXACT error message, there is still the possibility that the actual error is not this one. So it's best to sort this once for all rather than to take 3/4 messages to realize that this is not the actual issue.

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Thursday, December 1, 2011 10:17 AM
  • I need Sub2 to start few seconds after Sub1 finishes 

     

    Why ?

    IMO it could be rather a design problem. For example I saw once someone who paused to let the webbrowser load the document before resuming execution. Instead the webbrowser triggers an event when the document is loaded. So using this event rather than just to wait allows to trigger whatever he wanted as soon as possible.

    So understanding what you are doing with this delay could help...

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Thursday, December 1, 2011 10:34 AM