0

Got error on execution of following code snippets:

 Dim WithEvents Web As SHDocVw.InternetExplorer 
    Web = New SHDocVw.InternetExplorer

    Sub Web_BeforeNavigate2()
        Web.GetElementByID("ID").value = form1.grid1.get_textmatrix(0,0)

error: "current thread is not in a single-threaded apartment"

Sub Web_OnQuit()
    Me.Close()

error: Cross-thread operation not valid: Control 'frmVBbrowser' accessed from a thread other than the thread it was created on.

How to make InternetExplorer run in same thread ?

IT researcher
  • 3,274
  • 17
  • 79
  • 143
  • Please elaborate a few things for me: Are you actually starting different threads, or are these errors thrown regardless? What specific action causes the errors to be thrown? How do you attach the event handlers? [Where] are you accessing the `WebBrowser` control? In the `FormClosing` event? – Visual Vincent Feb 28 '19 at 06:29
  • @Visual Vincent threading is not used, `SHDocVw.InternetExplorer` object created inside a windows form. error comes if i access windows form/control inside IE objects events (may be due to separate thread created for IE object automatically) I wanted to close the form when internet explorer(object) is closed (all these code was working properly in vb6) – IT researcher Feb 28 '19 at 09:06
  • Possible duplicate of [How can I run code in a background thread and still access the UI?](https://stackoverflow.com/a/45571728/3740093) – Visual Vincent Feb 28 '19 at 17:21
  • I see. I have tested and can verify the issue. Please see the suggested duplicate. In my answer I thoroughly describe how accessing the UI from a background thread works in WinForms. This should solve both issues that you're experiencing. – Visual Vincent Feb 28 '19 at 17:21

0 Answers0