locked
Cross Thread Operation Not valid RRS feed

  • Question

  • UUGGHH, not another one...

    So im trying to make a listbox update via a 2nd thread and BOOM! my prog gets an invalid operation exception...

     

    Here is the code with the errors:

     

     ListBox1.Items.Remove(f)
     ListBox2.Items.Add(Application.StartupPath & "\Quarantine\" & q.Substring(ls + 1) & ".$av") 'The top one has no error, only this one... ?
    
    Any help would be appreciated...

    UPDATE:

    Now im getting errors for both lines, sometimes i get no errors, sometimes i get 1 or 2... Help?


    Well, If it is not worth it, it is not fun - you say programmers are boring but i say they are worth it. Thank them every day for making your life easier
    Tuesday, July 26, 2011 11:06 PM

Answers

  • You cannot interact with controls when in a thread other than the UI thread without invoking back to the UI thread.
     
    Look at the example on this link which is fairly similar to what you want to do:  http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx#Y450
     

     

    --
    Mike
    • Proposed as answer by Frank L. Smith Tuesday, July 26, 2011 11:20 PM
    • Unproposed as answer by Bob_Saget Tuesday, July 26, 2011 11:39 PM
    • Proposed as answer by Mike Feng Thursday, July 28, 2011 11:26 AM
    • Marked as answer by Mike Feng Friday, August 5, 2011 9:55 AM
    Tuesday, July 26, 2011 11:19 PM
  • Here is a very simple example.  Add a single listbox (listbox1) and a button (button1) to a form.  Populate the listbox with some strings.  This will add an item to the listbox from the second thread.  Expand this to suit your needs.
     
    Imports System.ComponentModel
    Public Class Form1
     Delegate Sub AddListItem(ByVal item As String)
     Public myDelegate As AddListItem
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      myDelegate = New AddListItem(AddressOf AddListItemMethod)
      Dim bw As New BackgroundWorker
      AddHandler bw.DoWork, AddressOf WorkerRoutine
      bw.RunWorkerAsync()
     End Sub
     Public Sub AddListItemMethod(ByVal item As String)
      ListBox1.Items.Add(item)
     End Sub
     Sub WorkerRoutine()
      Me.Invoke(Me.myDelegate, New Object() {"Bananas!"})
     End Sub
    End Class

    --
    Mike
    • Proposed as answer by Mike Feng Thursday, July 28, 2011 11:26 AM
    • Marked as answer by Mike Feng Friday, August 5, 2011 9:55 AM
    Wednesday, July 27, 2011 12:19 AM

All replies

  • it depends on what q is + what ls is
    thanks for any help
    Tuesday, July 26, 2011 11:14 PM
  • You cannot interact with controls when in a thread other than the UI thread without invoking back to the UI thread.
     
    Look at the example on this link which is fairly similar to what you want to do:  http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx#Y450
     

     

    --
    Mike
    • Proposed as answer by Frank L. Smith Tuesday, July 26, 2011 11:20 PM
    • Unproposed as answer by Bob_Saget Tuesday, July 26, 2011 11:39 PM
    • Proposed as answer by Mike Feng Thursday, July 28, 2011 11:26 AM
    • Marked as answer by Mike Feng Friday, August 5, 2011 9:55 AM
    Tuesday, July 26, 2011 11:19 PM
  • I do not understand some of this... Can you please provide me with a code example?
    Well, If it is not worth it, it is not fun - you say programmers are boring but i say they are worth it. Thank them every day for making your life easier
    Tuesday, July 26, 2011 11:39 PM
  • I do not understand some of this... Can you please provide me with a code example?
    Well, If it is not worth it, it is not fun - you say programmers are boring but i say they are worth it. Thank them every day for making your life easier


    Just because you don't understand it doesn't make his answer WRONG.

    In fact he's exactly right - you cannot [directly] access something on one thread from another.

    Why you UNmarked my proposed answer is beyond me - but best of luck.

    Tuesday, July 26, 2011 11:51 PM
  • I've tried the method from the link and it doesnt work

    Ugh


    Well, If it is not worth it, it is not fun - you say programmers are boring but i say they are worth it. Thank them every day for making your life easier
    Tuesday, July 26, 2011 11:54 PM
  • Here is a very simple example.  Add a single listbox (listbox1) and a button (button1) to a form.  Populate the listbox with some strings.  This will add an item to the listbox from the second thread.  Expand this to suit your needs.
     
    Imports System.ComponentModel
    Public Class Form1
     Delegate Sub AddListItem(ByVal item As String)
     Public myDelegate As AddListItem
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      myDelegate = New AddListItem(AddressOf AddListItemMethod)
      Dim bw As New BackgroundWorker
      AddHandler bw.DoWork, AddressOf WorkerRoutine
      bw.RunWorkerAsync()
     End Sub
     Public Sub AddListItemMethod(ByVal item As String)
      ListBox1.Items.Add(item)
     End Sub
     Sub WorkerRoutine()
      Me.Invoke(Me.myDelegate, New Object() {"Bananas!"})
     End Sub
    End Class

    --
    Mike
    • Proposed as answer by Mike Feng Thursday, July 28, 2011 11:26 AM
    • Marked as answer by Mike Feng Friday, August 5, 2011 9:55 AM
    Wednesday, July 27, 2011 12:19 AM