CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2006
    Posts
    25

    Accessing Forms controls from a timer callback

    I need to update Forms controls based on processing done in a timer delegate.

    This is what I get:

    A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

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

    Is there a workaround?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Accessing Forms controls from a timer callback

    Can you post the code you are using?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jun 2006
    Posts
    25

    Re: Accessing Forms controls from a timer callback

    Cilu:

    Thanks for your input, but it seems I found the solution. There is lots of code, so posting it would be problematic, but the problem was that I tried to access, say, "Label->Text=..." from a System::Timers::Timer->Elapsed callback which always runs on a separate thread.

    The solution is to use the controls' Invoke method to launch a delegate that would access the control from the UI thread.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured