Possible Duplicate:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on
I am working on some code where I initialize an object (in this case a form), which then idles until it either receives a message from someone, or one of its methods is invoked directly.
In my form, I have a listbox (lTester) that shows all calls made to this form. During run-time I get the following error:
Cross-thread operation not valid:
Control ‘lTester’ accessed from a
thread other than the thread it was
created on.
I have not started any threads manually, in fact I have never worked with them and don’t really know how C# manages the UI components internally. Is there any way to get around this?
If you want to know where your thread is comming from, you can place a break point in the method you know are being called (where the error is generated), open the Threads and Stack Trace windows in VS and see what initiated the call to begin with.
Even though the code is located in your Form class, the caller still desides which thread the call is executed in. That’s why, as Oskar stated, you have to check if you need to
Invoketo the main thread. There are tons of examples on how to do this, just GoogleInvokeRequiredand go from there.Let me know if you need an example.