1

I have an Outlook add-in that contains a web browser control that is a part of the user control at the moment and I am loading a webpage onload of this add-in. So, it works fine so far, however, when the page takes long to load, it makes the UI unusable for the user to continue with their work. So, my question really is, can I shift that web browser control from the user control and create a separate thread that has this web browser control and do all the web browser loading in that thread. This way, whilst the control is taking its time to load, the user can continue with their work.

I have been reading about this approach in this post: WebBrowser Control in a new thread

But I am struggling to reuse this for my particular use case. Could someone help me with this approach? I have posted my code here as well:

Web browser control loading Outlook unusable VB.Net

Community
  • 1
  • 1
Neophile
  • 5,660
  • 14
  • 61
  • 107
  • 2
    Your original question has a good comment: "There's lack of detail in the question to help you". Repeating the question and showing even less detail is not a smart way to use this web site. – Hans Passant Nov 28 '16 at 10:17
  • 1
    Could you please let me know what information I am missing? I'm more than happy to add more detail :). I've actually added most of my code in the other post. Not sure what more to add in terms of code. – Neophile Nov 28 '16 at 10:28

2 Answers2

0

I know that is not too much, but you can try something like that:

Declare a thread:

Private ThreadBrowse As Threading.Thread

Then start it:

Private Sub StartNavigate
    ThreadBrowse = New System.Threading.Thread(AddressOf TheWork)
    ThreadBrowse.SetApartmentState(Threading.ApartmentState.STA)
    ThreadBrowse.Start()
End Sub

This is a sub that the thread will do:

Private Sub TheWork()
   Dim IsFinish As Boolean = False
   WebBrowser1.Navigate(New Uri("http://stackoverflow.com"))

Recheck:
    WebBrowser1.InvokeCustom(Sub() IsFinish = (WebBrowser1.ReadyState = WebBrowserReadyState.Complete))
    If IsFinish = False Then
        Threading.Thread.Sleep(100)
        GoTo Recheck
    End If
End Sub

Don't forget to put his in a module:

<Runtime.CompilerServices.Extension()>
Public Sub InvokeCustom(ByVal Control As Control, ByVal Action As Action)
    If Control.InvokeRequired Then Control.Invoke(New MethodInvoker(Sub() Action()), Nothing) Else Action.Invoke()
End Sub

Maybe you can do better with this basic code, considerate like a "startup" :D

Tyler
  • 416
  • 4
  • 11
  • Would I be able to use my existing webbrowser control to do this? I did try something similar but using my existing webbrowser control (that is under the user control) doesn't really work. I'm guessing I'd need to create the web browser instance in the thread and then always run the web browser control in the thread. Would that work? – Neophile Nov 28 '16 at 11:54
  • Yes you can use for any control, just replace "WebBrowser1" with your webbrowser or your control. For create a webbrowser direct in a thread... mmh i must say that is a bit complicated, not impossible but complicated. I ran in this problem too, with others control is ok, but for a webbrowser is... "strange" :D Anyway test this code and let me know. – Tyler Nov 28 '16 at 11:57
0

Have you tried placing the web browser code in a separate class and then invoking the code?

CBoolMe
  • 41
  • 8