CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2009
    Posts
    116

    unhandled exception of type 'System.InvalidOperationException' occurred in System.Win

    When I tried to run my program, I saw the error dialog with description below pop up? Any idea how to troubleshoot it further?

    An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

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

  2. #2
    Join Date
    Oct 2015
    Posts
    26

    Re: unhandled exception of type 'System.InvalidOperationException' occurred in System

    You can not modify the GUI from a outside the GUI thread.

    You need to do something like this on WPF

    Code:
            /// <summary>
            /// Execute an action in a GUI thread
            /// </summary>
            /// <param name="element">UIElement requiring GUI thread</param>
            /// <param name="action">action to execute</param>
            public static void SafeExcuteActionSync(UIElement element, Action action)
            {
                if (element == null || element.Dispatcher.CheckAccess())
                {
                    action?.Invoke();
                }
                else
                {
                    element.Dispatcher.Invoke(action);
                }
            }
    
            /// <summary>
            /// Execute an action in the GUI  thread async
            /// </summary>
            /// <param name="element">UIElement requiring GUI thread</param>
            /// <param name="action">action to execute</param>
            async public static void SafeExcuteActionASync(UIElement element, Action action)
            {
                if (element == null || element.Dispatcher.CheckAccess())
                {
                    action?.Invoke();
                }
                else
                {
                    await element.Dispatcher.InvokeAsync(action);
                }
            }
    for Windows forms use something like
    Code:
       if (mycontrol.InvokeRequired)
       { 
           mycontrol.Invoke(SomeFunction, parameter);
       }
       else
       {
          SomeFunction(parameter);
       }

  3. #3
    Join Date
    Oct 2015
    Posts
    26

    Re: unhandled exception of type 'System.InvalidOperationException' occurred in System

    Run your program in the debugger and break when the dialog appears. This will show you where you need to change your code to use the above code

  4. #4
    Join Date
    Apr 2009
    Posts
    116

    Re: unhandled exception of type 'System.InvalidOperationException' occurred in System

    Yes, that was what I did. However, still cannot figure out the root cause.

  5. #5
    Join Date
    Apr 2009
    Posts
    116

    Re: unhandled exception of type 'System.InvalidOperationException' occurred in System

    Thanks for the advice. May I know what is (mycontrol.InvokeRequired)?
    Regards,
    PH Chang

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: unhandled exception of type 'System.InvalidOperationException' occurred in System

    Quote Originally Posted by PHChang View Post
    Yes, that was what I did. However, still cannot figure out the root cause.
    Does your program process incoming data from the network or a port? If so, the handler that processes the data is running on a different thread. Notice that you may not have explicily created the thread yourself, but it may have been done for you. At any rate inside the handler is where you are trying to use the control and the error is telling you that the control was created on a different thread. You need to access the control with the Invoke method as suggested.

  7. #7
    Join Date
    Apr 2009
    Posts
    116

    Re: unhandled exception of type 'System.InvalidOperationException' occurred in System

    Thanks for your advice. Yes, my program is processing incoming data from network. I have no idea how to get access to the control with the Invoke method as suggested. However, if I tried to debug using remote debugging, I will not see this issue.
    The problem with remote debugging is that I am having problem stepping into code that has breakpoint set.
    It showed the message "The breakpoint will not be currently hit. No executable code is associated with this line. Possible causes include: preprocessor directives or compiler/linker optimization" (as shown in the picture I attached)
    I did copied the pdb file over to the remote PC for debugging. Just wondering what I am missing here.
    If cannot use the debug mode, I can only add logging for troubleshooting issues with my program for the time being.
    Attached Images Attached Images  

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