locked
update UI from different thread RRS feed

  • Question

  • Hi everyone

    Im working on a program ( vb 2005 CE framework ). I have mainly two threads, the main one and another one I use it for a different thing.

    When I tried to update the UI this exception was thrown : Control.Invoke must be used to interact with controls created on a separate thread.

    well ! I ve looked into it on internet, people were saying things like you can't update the UI from any other thread than its own thread, which is logical. My problem is !!! I have tried to use the INVOKE, but it wouldn't work. Please if you have any suggestions, let me know.

    this is  where I get the error :

     

     

      Public Sub PaintKeys()
        Dim tempLevel As clsLevel
        Dim tempKey As ClsKey
    
        objScroll = New clsScroll
         For i As Byte = 0 To MAX_KEYS - 1
          FrmHome.Controls.Remove(objBtn(i)) ' <=== HERE
        Next
    
        Application.DoEvents()
    
        tempLevel = Item(varActiveLevel)
        For i As Integer = 1 To tempLevel.Count() ' pass TRUE value as parameter to use the NEW METHOD
    
          tempKey = tempLevel.Item(i)
          PaintKey(tempKey.position, tempKey.number)
        Next
      End Sub
    
    

     

    Thank you.

     

    Tuesday, June 15, 2010 11:25 AM

Answers

All replies

  • Hi MehdiNet,
    What you read is technically correct, however there is a way to do this, you have to create a Delegate to update the main form, here is a small example:

      Public Delegate Sub RemoveControlDelegate(ByVal Ctrl As System.Windows.Forms.Control)
    
      Public Sub RemoveControl(ByVal Ctrl As System.Windows.Forms.Control)
        If Me.InvokeRequired Then
          Dim Del As New RemoveControlDelegate(AddressOf RemoveControl)
          Me.Invoke(Del, Ctrl)
        Else
          Me.Controls.Remove(Ctrl)
        End If
      End Sub
    

    You would add this code to your "FrmHome", and then from you PaintKey method you would call FrmHome.RemoveControl(objBtn(i)).

     

    this code works by checking to see if you are calling the code form the UI thread (Me.InvokeRequired) and then if you are not using the delegate to call back to the UI thread and invoking the same method.  This makes so you are correctly calling the remove method from the correct thread. 

     

    If you have any other question please let me know.


    Adam - Please remember to mark the answers.
    Tuesday, June 15, 2010 11:52 AM
  • Hi Adam thanks very much for your reply.

    Ive just tried it, and it keeps saying :  Control.Invoke must be used to interact with controls created on a separate thread.

    but in a different line ( Me.Invoke(Del, Ctrl) )

    Could it be because I call FrmHome.RemoveControl(objBtn(i)) in a loop ??

      Public Sub PaintKeys()
        Dim tempLevel As clsLevel
        Dim tempKey As ClsKey
    
        For i As Byte = 0 To MAX_KEYS - 1
          FrmHome.RemoveControl(objBtn(i))
        Next
    
        Application.DoEvents()
        tempLevel = Item(varActiveLevel)
        For i As Integer = 1 To tempLevel.Count() '
          tempKey = tempLevel.Item(i)
          PaintKey(tempKey.position, tempKey.number)
        Next
      End Sub
    I really appreciate your help Adam, thaks

    Tuesday, June 15, 2010 10:01 PM
  • I want to make sure i am telling you the right thing.

    Where is FrmHome declared?

    What does the PaintKey Method do to add the controls to the FrmHome?

    What I'm thinking is happening is the app is getting confused by the way you are adding the controls to the form the PaintKey Method.


    Adam - Please remember to mark the answers.
    Wednesday, June 16, 2010 11:18 AM
  • Hi Adam

    well !

    FrmHome is the main Form, basically declared on the main thread.
    PaintKey method is a member of the class clsKey , which is actually running on the same thread, but from time to time I receive some data from another terminal through the SOCKET class ( the communication is done on another thread ) . When all the data is received, I call this method again in order to update my screen ( it's a sort of real-time system ).

    PS: the PaintKey method works perfectly fine before receiving the data ( this could imply single-thread)

    I don't know if this helps, but I wish you could come up with an idea, meanwhile I am trying to sort it out as well, and I 'll let you know.

    Many thanks

    Matt

    Thursday, June 17, 2010 12:15 AM
  • Hi Adam, thanks for your help. the problem has been resolved, if you want to know how, have a look at this :

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/50a13d20-1ef5-48c9-8a28-f7cff84917a0

     

    many thanks 

    Matt

    • Marked as answer by Liliane Teng Tuesday, June 22, 2010 5:35 AM
    Friday, June 18, 2010 9:54 AM