I have an application in which I am running a separate thread.
Dim thread As New System.Threading.Thread(AddressOf Main)
thread.Start()
However, the thread makes reference to an textbox named Output, and it generates this error upon execution:
System.InvalidOperationException was unhandled
Message="Cross-thread operation not valid: Control 'Output' accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
(message shortened for space)
How can I make the operations run on another thread, but still use the Output object? I cannot call a subroutine to do that for me, as it generates the exact same error.
The method called is AppendText, by the way.
I am probably missing something important here, so thanks for the help!
Instead of just calling the
AppendTextmethod you need to force it to execute on the correct thread. So, if you have a call like this:…you need to change it to this:
You can use either
InvokeorBeginInvoke. In this case, sinceAppendTextdoesn’t have any return value,BeginInvokeis a good choice (the difference is thatInvokewill block the current thread while the GUI thread executes theAppendTextmethod, whileBeginInvokewill make the call asynchronously instead).