Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would a control be on a different thread from the one I'm working in?

I'm not (intentionally) using threads in my C# app. In fact my main method has a [STAThread], which I thought meant I'd only be using one thread.

Why then, would I be getting this error message?

Cross-thread operation not valid: Control 'messageLog' accessed from a thread other than the thread it was created on.

like image 296
Tom Wright Avatar asked Feb 27 '23 19:02

Tom Wright


1 Answers

There are a couple of types which can cause your code to run on different threads without any explicit call to System.Threading. In particular FileSystemWatcher and BackgroundWorker come to mind. Are you using any of these types?

Also STAThread in no way limits the ability of your process to spawn threads. It instead sets the COM apartment type of the initial application thread.

like image 115
JaredPar Avatar answered Mar 01 '23 12:03

JaredPar