0

So I have multiple threads running in vb.net. I need to know, how does one add to a listbox that was "created in a different thread"? I can't seem to get past this error!

Exact Error:

    System.InvalidOperationException occurred
  HResult=0x80131509
  Message=Cross-thread operation not valid: Control 'ListBox2' accessed from a thread other than the thread it was created on.
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.ListBox.NativeAdd(Object item)
   at System.Windows.Forms.ListBox.ObjectCollection.AddInternal(Object item)
   at System.Windows.Forms.ListBox.ObjectCollection.Add(Object item)
   at SuperBC_Check_Pro.Form1.IncluBC(String username, String membershipType) in C:\Users\Owner\documents\visual studio 2017\Projects\SuperBC-Check Pro\SuperBC-Check Pro\Form1.vb:line 64
   at SuperBC_Check_Pro.Form1.CheckBCMembership() in C:\Users\Owner\documents\visual studio 2017\Projects\SuperBC-Check Pro\SuperBC-Check Pro\Form1.vb:line 92
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

What do you guys suggest I go about fixing such an error?

Ethan
  • 42
  • 1
  • 6
  • Possible duplicate of [How to update the GUI from another thread in C#?](https://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – rkosegi Jul 29 '17 at 06:27
  • You generally can't affect the UI from anything but the UI thread. You need to marshal a method call to the UI thread first and then perform your action there. Here's a breakdown of the steps to do that: http://www.vbforums.com/showthread.php?498387-Accessing-Controls-from-Worker-Threads&highlight= – jmcilhinney Jul 29 '17 at 09:52
  • [**Performing thread-safe calls using Control.Invoke()**](https://stackoverflow.com/documentation/vb.net/1913/threading/6235/performing-thread-safe-calls-using-control-invoke). – Visual Vincent Jul 29 '17 at 09:59

0 Answers0