Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross thread operation not valid

im trying to access a rich textbox on another form im using the following code to do so:

Private Delegate Sub StringDelegateChat(text As String, window As ChatWindow)
    Private Sub AppendTextChatWindows(text As String, window As ChatWindow)
        Try              
            If window.RichTextBox1.InvokeRequired Then
                window.Invoke(New StringDelegateChat(AddressOf AppendTextChatWindows), text, window)
            Else
                window.RichTextBox1.AppendText(text)
                window.RichTextBox1.SelectionStart = window.RichTextBox1.Text.Length
                window.RichTextBox1.ScrollToCaret()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

but i get the cross thread operation not valid error, i think it does this because it misses out the window.invoke part of the if statement. i also tried replacing theIf window.RichTextBox1.InvokeRequired Then to If InvokeRequired Then but it gets caught in a continues loop and a stack overflow error is thrown.

Thanks Houlahan

like image 564
Houlahan Avatar asked Oct 12 '11 16:10

Houlahan


People also ask

What is cross thread operation?

Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on. April 5, 2016 3 Comments. This error occurs when trying to perform operations on a windows control when using threading.

What is cross threading in C#?

In the first line we get to know whether the control we want to access is created on the same thread or another thread. If it's created on another thread then it will execute the second line.

What is InvokeRequired in C#?

Gets a value indicating whether the caller must call an invoke method when making method calls to the control because the caller is on a different thread than the one the control was created on. public: property bool InvokeRequired { bool get(); }; C# Copy.


2 Answers

I believe, on line 5, window.Invoke should be changed to window.RichTextBox1.Invoke.

Private Delegate Sub StringDelegateChat(text As String, window As ChatWindow)
Private Sub AppendTextChatWindows(text As String, window As ChatWindow)
    Try
        If window.RichTextBox1.InvokeRequired Then
            window.RichTextBox1.Invoke(New StringDelegateChat(AddressOf AppendTextChatWindows), text, window)
        Else
            window.RichTextBox1.AppendText(text)
            window.RichTextBox1.SelectionStart = window.RichTextBox1.Text.Length
            window.RichTextBox1.ScrollToCaret()
        End If
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
End Sub
like image 143
Timiz0r Avatar answered Oct 17 '22 10:10

Timiz0r


Have you tried:

    Private Sub AppendTextChatWindows(text As String, window As ChatWindow)
        Try              
            If window.RichTextBox1.InvokeRequired Then
                window.RichTextBox1.BeginInvoke(New StringDelegateChat(AddressOf AppendTextChatWindows), text, window)
                Exit Sub 
            Else
                window.RichTextBox1.AppendText(text)
                window.RichTextBox1.SelectionStart = window.RichTextBox1.Text.Length
                window.RichTextBox1.ScrollToCaret()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

Basically, I am asking about BeginInvoke rather than Invoke. Although I would expect, as another poster mentioned, that you should be using the same thing you check the required against to invoke. (ie both window.invokeRequired & window.BeginInvoke or the control)

like image 3
Jay Avatar answered Oct 17 '22 09:10

Jay