Cross Thread Headache

I am having a cross thread headache with my control I have created.

I have a control on a form (frmUtils) when an event is raised by this control I want to update a listbox on my main form (frmMain) to show this data.

I have read a lot of posts about this problem but nothing seems to give me an elegant solution.

Setting CheckForIllegalCrossTheadCalls=False, causes other code sections to stop working properly. It is very frustrating when something that was so simple in VB6 is suddenly more than a days work in 2005 Does anyone have an comments / thoughts

Thanks


Answer this question

Cross Thread Headache

  • Kaloyan Georgiev


    Private Sub control_Event(ByVal sender As Object, ByVal e As System.EventArgs)
    If InvokeRequired Then
    Dim
    arguments() As Object = New Object() {sender, e}
    Dim method As EventHandler = AddressOf Me.control_Event
    Invoke(method, arguments)
    Return
    End If
    lblProcess.Text = "Control event fired!"
    End Sub


    You can use the InvokeRequired property to lookup if this method must be invoked.


  • AaronSchurg

    PJ. van de Sande wrote:

    Private Sub control_Event(ByVal sender As Object, ByVal e As System.EventArgs)
    If InvokeRequired Then
    Dim
    arguments() As Object = New Object() {sender, e}
    Dim method As EventHandler = AddressOf Me.control_Event
    Invoke(method, arguments)
    Return
    End If
    lblProcess.Text = "Control event fired!"
    End Sub



    You can use the InvokeRequired property to lookup if this method must be invoked.

    Hi, I'm very sorry but I'm a bit slow to follow your code and on top of that I'm not very familiar with neither VB nor with object oriented and event driven programming. (I'm old school). I have the following function which is called from a different thread than the textbox1 is in.

    sub update(byref s as string)

    textbox1.text = s

    end sub

    I pretty much would like to use your approach with "invokerequired", however I don't quite get it how to handle this argument. Would it be possible for you to rewrite my sub and show me how to use the Invoke approach to make "update" cross thread proof.

    Thank you very much

    Daniel


  • Vantskruv

    I disagree, because it is overhead if you don't need it!


  • Sébastien Nunes

    I figured out what to do. I deleted the thread since I have plenty of memory. Now I don't have any problems! Maybe in the future MS will fix the Textbox control so that it handles any thread problems. This would be more in keeping with the visual basic concept.
  • corado

    I'm altering the DirectX DirectSound program CaptureSound which starts a thread to for monitoring the CaptureBuffer. This program has a form with buttons on it. The buttons work fine. I put a checkbox on it and that works fine. Debug.print works fine. I put a TextBox on it and when I ask the program to write something to the TextBox it halts the program with a cross-thread alert. I have been reading stuff about this and all the fixes look like they should be incorporated into the basic textbox control just like they must be in the checkbox and button controls. Is there a generic fix for textboxes

    Public Sub StartCapture()

    InitDirectSound()

    If Nothing Is applicationDevice Then

    Close()

    Else

    CreateCaptureBuffer()

    k = k + 1

    Debug.Print(k)

    TextBox1.Invoke()

    TextBox1.Text = k

    applicationBuffer.Start(True)

    'to read data see RecordCapturedData. Then lookup CaptureBuffer Class and Read and array read (second listed sub).

    End If

    End Sub


  • RudolfT

    This is a much simpler (and neater) solution than I have seen before - and it works - many thanks.

    Could you give me a quick explanation of how it works and what is happening at each stage

    Matt

  • Doug Ramirez

    InvokeRequired is a check to see if u can call a method from another thread.

    What PJ does is he checks if invoking is required ( duhh ) on a method from another form. If it is true he sets the arguments( the sender object and the specified arguments. After that he create's a new eventhandler with the address of the caller.

    Then he calls the method Invoke, this method calls the method specified in the EventHandler to execute. The Invoke method executes the method specified by the delegate (EventHandler) on the thread where the form was initialized.

  • Cross Thread Headache