Cross-Thread operation not valid in WinForm


One problem while working with Threads in C# is that a background thread cannot ‘talk’ to the any visual elements that exists in the main GUI thread, because talking to objects across thread boundaries is forbidden. The reasons are obvious; because a thread has its own process space, the address of an object in one thread is not useful as an address in another thread. As a result, trying to access that object in another space results in a program crash with an unhandled exception. However, C# provides a mechanism for working with cross-thread process issue.

The BeginInvoke method of the MethodInvoker delegate is used within a thread prcoess to call another process method. These methods are thread-safe and can be used in a multi-threaded environment.

Last night, when a Thread tried to access the Text property of a Label control. I got the following error:

Cross-Thread operation not valid: Control ‘label3’ accessed from a thread other than the thread that it was created on.

Add the below quoted codes to perform a cross-thread operation.

   1: if (label3.InvokeRequired)
   2: {
   3:     label3.Invoke(new MethodInvoker(delegate{label3.Text = "sample text";});
   4: }


Hope this helped.

Thanks Winking smile

 

VSAchievements

Visual Studio Achievements
abhilashca (132 Points)
  • Don't Try This At Home
    • Magic Numbers
      Magic Numbers (0 Points)
      Write a enum with 30 fields. Who needs numbers when you've got words! Uses FxCop
      2 days ago
    • Overload
      Overload (0 Points)
      More than 10 overloads of a method. You could go with this or you could go with that. Uses FxCop
      Wednesday, February 13, 2013
    • Go To Hell
      Go To Hell (0 Points)
      Use of the goto keyword. Um, I heard it was a best practice that you weren't supposed to do that anymore.
      Friday, January 04, 2013
    • Scroll Bar Wizard
      Write a single line of 300 characters long. Who needs carriage returns? Uses FxCop
      Thursday, December 27, 2012
  • Power Coder
    • Time For An Upgrade
      A solution takes 10 minutes to compile. Dag, that's one enormous solution!
      Friday, May 03, 2013
    • Suspicious
      Suspicious (10 Points)
      Use 5 preprocessor directives. Compiler commands -- aren't you fancy!
      Friday, February 22, 2013
    • Equal Opportunist
      Equal Opportunist (10 Points)
      Write a class with public, private, protected and internal members. It's all about scope. Uses FxCop
      Monday, January 21, 2013
    • On The Shoulders of Giants
      Reference 25 assemblies. Hey, why should you write it if someone else already did?
      Tuesday, January 08, 2013
    • Localization Guru
      Localization Guru (10 Points)
      Have 1000 localized values. Nice localization work!
      Thursday, January 26, 2012
  • Just For Fun
    • Save A Tree
      Save A Tree (5 Points)
      Print source code. My boss told me to. I swear!
      Monday, March 04, 2013
    • Obsessive Compulsive Disorder (OCD)
      Invoke the 'Close All But This' menu option 10 times. I mean, who invented those damn tabs anyway?
      Sunday, February 26, 2012
    • Lonely
      Lonely (5 Points)
      Code on a Friday or Saturday night. Coding? Tonight? Ouch.
      Saturday, February 04, 2012
    • Install and Register For Visual Studio Achievements
      Install the add-in and register with Channel9. You are up and running!
      Sunday, January 22, 2012
  • Windows 8
  • Unleashing Visual Studio
    • The Explorer
      The Explorer (5 Points)
      Start a debug session using step into (F11) more than 10 times. Ah, the power of F11. Every coder's best friend.
      Monday, February 27, 2012
    • Casual Observer
      Casual Observer (5 Points)
      Start a debug session using step over (F10) more than 10 times. I thank the debugger every day.
      Saturday, February 11, 2012
  • Good Housekeeping
    • Regional Manager
      Regional Manager (7 Points)
      Add 10 regions to a class. Your code is so readable, if I only didn't have to keep collapsing and expanding!
      Thursday, February 02, 2012
  • Customizing Visual Studio
    • Extensions Junkie Deluxe
      Install 10 extensions to Visual Studio. How can you find anything on a menu?
      Sunday, January 22, 2012
    • Extensions Junkie
      Install 5 extensions to Visual Studio. Extensibility rocks!
      Sunday, January 22, 2012

Month List

Tag cloud