RDP Session Causes OnStartup Call Each Time Reconnect Happens (C# and WPF)

  • Thread starter Thread starter JohnV1982
  • Start date Start date
J

JohnV1982

Guest
I know that this has been asked before but I did not find a satisfactory solution to the problem in hand.

It seems like when a user reconnects to a Windows Server 2008/2016 Session using Remote Desktop (RDP protocol), this causes a call to the "OnStartup(StartupEventArgs e)" method in many WPF applications.

Snippet of Code:

protected override void OnStartup(System.Windows.StartupEventArgs e)
{
base.OnStartup(e);
string AppDomainName = "My new domain";

ShutdownMode = ShutdownMode.OnLastWindowClose;
var my_domain = AppDomain.CreateDomain(AppDomainName, AppDomain.CurrentDomain.Evidence, AppDomain.CurrentDomain.SetupInformation);

CrossAppDomainDelegate MyActionLoad = () =>
{
Thread thread = new Thread(() =>
{
var app = new Application();
app.Run(); // <-- THIS CRASHES WHEN RDP IS RE-ESTABLISHED.
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
};

my_domain.DoCallBack(MyActionLoad);
my_domain.DoCallBack(() => Application.Current.Dispatcher.Invoke(new Action(Application.Current.Shutdown)));

new Action(
() =>
{
Thread.Sleep(500);
AppDomain.Unload(my_domain);

}).BeginInvoke(null, null);

}




3 QUESTIONS:

1) Why is RDP triggering this call to OnStartup method? (And why, no matter what condition I put in the source code before it, it always crashes in the app.Run() statement, even if I check for conditions, write to a file and check that the file exists as a way to prevent calling this run method again, etc).

2) Is this a bug in Windows Server Server 2016? (But I have seen this behavior happen in Windows Server 2008). My server is on Microsoft Windows Server 2016 Standard (Version 10.0.14393). Is there a patch or service pack or something to fix this problem?

3) Any recommendation to avoid having the "app.Run()" method in the "OnStartup" to get triggered/called AFTER someone reconnects to the server via RDP?

Thank you.

Continue reading...
 
Back
Top