problem with threads between framework 1.1 and 2.0

Hi people,

My small applicaton is working well on framework 1.1 but when I use 2.0 beta, it's giving me an exception: 

Illegal cross-thread operation: Control accessed from a thread other than the thread it was created on.

Does anyone have an idea about this

thanks,
talal


Answer this question

problem with threads between framework 1.1 and 2.0

  • Chris Nurse

    One of the main rules of Windows programming is that you must not access a visual element from a thread other than the one that created it.  Version 1.1 of the framework trusts that the programmer won't attempt it.  Cross-threading bugs can be difficult to spot if you don't know that's the problem, so version 2.0 throws the exception you mentioned to try to force you do do it right.

    If controlInQuestion.InvokeRequired returns true, you are on a different thread and you should call Invoke on the control rather than executing the method directly.

  • John Murdoch

    Hi,

    Thanks for the tip. I managed to make it work using ContolInstanceName.BeginInvoke(delegateName, objet[] methodParameters) 

    What is the exact difference between Invoke and BeginInvoke  and if we do BeginInvoke, do we have to use EndInvoke after that

    talal

  • problem with threads between framework 1.1 and 2.0