12

When my WPF application is open, and I open a Remote Desktop Connect to my PC, WPF reloads the application (Unload and Load events on the main control are triggered). When I disconnect, this happens again.

I understand that the UI has to be redrawn, but why is the control reloaded? Can this be prevented? Or is there a way to detect if a reload is triggered by an RDP (dis)connect?

Coder14
  • 1,305
  • 1
  • 9
  • 26
  • what kind of control that gets reloaded? Please provide more detail.. – Eriawan Kusumawardhono Dec 03 '15 at 08:55
  • Related (not a duplicate): [Win8.1 will fire unload and load event to the WPF application when we close and reconnect to this machine using RDC from win7 or other OS](https://stackoverflow.com/q/24545853/1364007). – Wai Ha Lee Mar 27 '20 at 12:07

2 Answers2

2

I could fix the problems with my application by unsubscribing the Loaded-event at first execution and move deinit code from the Unloaded-event to the Exit-event.

Coder14
  • 1,305
  • 1
  • 9
  • 26
  • I'm looking to implement your solution but I met several issues: The constructor of my main control is always called. independantly of the loaded event. How do you properly unsubscribe to the "load" event. are you speaking of the loaded event ? (which appears after the constructor call) – Tchoupi Jun 02 '20 at 13:18
  • Subscribe to the Loaded event, and unsubscribe inside the handler. – Coder14 Jun 03 '20 at 08:20
  • Thanks for the reply, I've already tried it on my windows and on my control itself. The problem is the loaded event is called after the Constructor call of my control. so the control itself is rebuilt which lead to invalid behaviors. in my constructor if I only initialize one time (to avoid the refresh) and do not call the InitializeComponent() method, the control is not rendered and is display as a black element – Tchoupi Jun 04 '20 at 12:10
  • InitializeComponent should stay in the constructor. It was custom initialization I moved to the Loaded event handler. – Coder14 Jun 04 '20 at 17:25
1

I don't know why this happens, but with some code from this page: Optimizing Visual Studio 2010 and WPF applications for Remote Desktop I think that you can understand if (Un)Loaded is triggered by Remote Desktop.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Giangregorio
  • 1,482
  • 2
  • 11
  • 12