Threads

J

Jakub Cermak

Hi all,
sorry for my beginner question.
How can I change a form property from a thread (I don't think primary
application thread, but the instance of Thread class)? If I do it i get
an exception InvalidOperationException - Cross-thread
operation not valid: Control 'lblStatusGame' accessed from a thread
other than the thread it was created on.
--
Best regards,

Jakub Cermak
ICQ 159971304
http://cermi.wz.cz
 
J

Jon Skeet [C# MVP]

Jakub Cermak said:
sorry for my beginner question.
How can I change a form property from a thread (I don't think primary
application thread, but the instance of Thread class)? If I do it i get
an exception InvalidOperationException - Cross-thread
operation not valid: Control 'lblStatusGame' accessed from a thread
other than the thread it was created on.

You can't - you need to use Control.Invoke or Control.BeginInvoke.

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml
 
D

Dmytro Lapshyn [MVP]

Hi Jakub,

All operations on forms and controls must be done on the main (UI) thread.
If you need to do them from other threads, use the Control.InvokeRequired
and Control.Invoke (see MSDN docs on these for detailed information).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top