Results 1 to 4 of 4

Thread: System.InvalidOperationException: Cross-thread operation not valid

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    System.InvalidOperationException: Cross-thread operation not valid

    All,

    I have a main VB form (frmMain). frmMain has-a subForm (frmOPC), and declares a private member of type frmOPC WithEvents.

    VB Code:
    1. ' frmMain
    2. Option Explicit
    3. ' ...
    4. ' This is my SubForm which can Raise an Event
    5. Private WithEvents m_frmOPC as frmOPC

    I wish frmMain to respond graphically to an Event Raised by frmOPC, such as update a frmMain.Label.Text = "something" when frmMain captures an Event Raised by its subForm, frmOPC.

    VB Code:
    1. Private Sub m_frmOPC_OPC_DataWriteVerified(ByVal intLaneNum As Integer) Handles m_frmOPC.OPC_DataWriteVerified
    2.     ' Do something interesting on the screen ...
    3.     Me.lblOPC_Result.Text = "Lane " & strLane & " data verified (" & strTime & " mSecs)."
    4.     ' This causes an error!
    5. End Sub

    The SubForm has-a VB6-developed ActiveX control which Raises an Event.

    VB Code:
    1. ' frmOPC
    2. Option Explicit
    3. Imports ActiveX_Library
    4. ' ...
    5. ' This is my ActiveX Object which can Raise an Event
    6. Private WithEvents m_OPC_Process As ActiveX_Library.OPC_Process.cls_OPC_Group

    Obviously, when the VB6 ActiveX Object Raises an Event, I capture this Event, and in turn, Raise my own Event:

    VB Code:
    1. Private Sub m_OPC_Process_DataChanged(ByRef Context As String, ByRef TagName As String, ByRef TagValue As String) Handles m_OPC_Process.DataChanged
    2.     RaiseEvent OPC_DataWriteVerified(m_intLaneNum)
    3.     ' Notice, the frmMain is trying to update it's screen right now I think, before this Event has finished...
    4. End Sub

    The problem? I'm glad you asked! frmMain will puke a hard-to-track-down error if I try to mutate an object on the Form:

    Code:
    System.InvalidOperationException: Cross-thread operation not valid: Control 'lblOPC_Result' accessed from a thread other than the thread it was created on.
       at System.Windows.Forms.Control.get_Handle()
       at System.Windows.Forms.Control.set_WindowText(String value)
       at System.Windows.Forms.Control.set_Text(String value)
       at System.Windows.Forms.Label.set_Text(String value)
       at RUUD_Production_Supervisor.frmMain.m_frmOPC_OPC_DataWriteVerified(Int32 intLaneNum) in E:\Work\Visual Studio 2005\Projects\RUUD_Production_Supervisor\RUUD_Production_Supervisor\frmMain.vb:line 188
    Interestingly, I can do anything non-graphical with no issues. The error is only thrown when I try to make changes to the Form's screen Objects.

    I found a workaround, which is always green squiggled as a warning in the IDE at design-time, but effectively removes the erroneous behavior (no error is thrown):

    VB Code:
    1. Private Sub frmOPC_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2.     '
    3.     ' ...
    4.     Me.CheckForIllegalCrossThreadCalls = False
    5.     '
    6. End Sub

    I include a screenshot of the green squiggle tooltip/warning.

    I am looking for ideas and suggestions for a better solution to the problem than my work-around, if one exists. Questions for clarification welcome.
    Attached Images Attached Images  
    Last edited by Dave Sell; Sep 14th, 2009 at 03:40 PM.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: System.InvalidOperationException: Cross-thread operation not valid

    1. Set a control's CheckForIllegalCossThreadCalls property to False is generally not recommended.
    2. To properly access a control from a different thread, you use delegate. Search the forum for "illegal cross thread" and you should find many examples showing you how to do it.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: System.InvalidOperationException: Cross-thread operation not valid

    This is very similar to this thread: http://www.vbforums.com/showthread.php?t=583598

    Perhaps you can learn something from that thread or help that guy out as you both appear to be using the same system (this OPC thing) ?

    EDIT: Here's the thread that shows how to access controls from background threads: http://www.vbforums.com/showthread.php?t=498387
    Last edited by chris128; Sep 14th, 2009 at 04:36 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: System.InvalidOperationException: Cross-thread operation not valid

    As stanav says, setting CheckForIllegalCrossThreadCalls to False is not generally recommended. FYI though, the reason that you get that green squiggle is because, as the error message suggests, that property is Shared. That means that you call it on the Control class, not on some instance of the Control class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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