On remote desktop machine (Windows 11), the DataGrid (WPF C# application .Net Framework 4.7.2) event is throwing System.Reflection.TargetInvocationException.

Sharayu Mahadeorao P 0 Reputation points
2024-07-01T14:03:37.68+00:00

The DataGrid event is giving the exception"System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Added item does not appear at given index '4'. " while running the code on remote desktop machine (Windows 11) but same code is not giving any exception on local machine (Windows 11).

Below is the observation:

  1. DataGrid_Load is getting fired when remote desktop is reconnected.
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,011 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 54,811 Reputation points
    2024-07-02T02:38:57.1266667+00:00

    Sounds like a race condition to me and nothing really to do with RD. However your explanation is confusing to me. The Load event wouldn't trigger when RD is closed unless something is triggering the UX to load as that event is called when the window is displayed.

    Given your explanation it is my guess that you have a bad event handler. Most likely the property changed event is being raised on an object that is getting cleaned up and therefore it crashes. You cannot replicate this problem easily locally because you cannot close out your session and keep the debugger attached locally.

    Please post the stacktrace that is being reported as part of this exception. Most likely you need to disconnect your event handlers before the window closes but the stack will help show that.

    Also please clarify whether you're closing the window (and leaving the user connected in the session) or closing the session AND logging off the user at the same time.


  2. Hongrui Yu-MSFT 2,465 Reputation points Microsoft Vendor
    2024-07-02T08:29:44.59+00:00

    Hi,@Sharayu Mahadeorao P.

    Reason: When you reconnect remotely, the page will be re-rendered for you, and the triggering conditions of the Loaded event are met, so the Loaded event is executed again.

     

    Solution: You can choose to use the Initialized event instead of the Loaded event

     

    You can learn more about the difference between Initialized event and Loaded event. Below is their official link

     

    Loaded event: FrameworkElement.Loaded Event (System.Windows) | Microsoft Learn

    Initialized event: FrameworkElement.Initialized Event (System.Windows) | Microsoft Learn

     

    The error you mentioned is not found in the code you provided. I guess it is caused by executing the Loaded event again. If necessary, please explain in detail.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.