From e74773a73df09385d784765c77f49272c986b29a Mon Sep 17 00:00:00 2001 From: Ken Morse Date: Thu, 28 Jun 2018 11:36:58 -0400 Subject: [PATCH] Delay setting up RequestReceived handler Delay setting up RequestReceived handler until both pathways (Edge and desktop bridge app) in OnBackgroundActivated are initialized. --- SecureInput/NativeMessagingHostInProcess/App.xaml.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/SecureInput/NativeMessagingHostInProcess/App.xaml.cs b/SecureInput/NativeMessagingHostInProcess/App.xaml.cs index ea69769..0b36e92 100644 --- a/SecureInput/NativeMessagingHostInProcess/App.xaml.cs +++ b/SecureInput/NativeMessagingHostInProcess/App.xaml.cs @@ -28,6 +28,7 @@ sealed partial class App : Application { private BackgroundTaskDeferral appServiceDeferral = null; private AppServiceConnection connection = null; + private AppServiceConnection edgeConnection = null; private BackgroundTaskDeferral desktopBridgeAppServiceDeferral = null; private AppServiceConnection desktopBridgeConnection = null; private bool desktopBridgeAppLaunched = true; @@ -77,13 +78,16 @@ protected async override void OnBackgroundActivated(BackgroundActivatedEventArgs desktopBridgeAppServiceDeferrals.Add(desktopBridgeConnectionIndex, this.desktopBridgeAppServiceDeferral); desktopBridgeConnectionIndex++; } + + // wait before configuring RequestReceived + this.edgeConnection.RequestReceived += OnAppServiceRequestReceived; } else // App service connection from Edge browser { this.appServiceDeferral = taskInstance.GetDeferral(); // Get a deferral so that the service isn't terminated. taskInstance.Canceled += OnAppServicesCanceled; // Associate a cancellation handler with the background task. this.connection = appService.AppServiceConnection; - this.connection.RequestReceived += OnAppServiceRequestReceived; + this.edgeConnection = this.connection; this.connection.ServiceClosed += AppServiceConnection_ServiceClosed; lock (thisLock) @@ -120,7 +124,10 @@ private async void OnAppServiceRequestReceived(AppServiceConnection sender, AppS { if (this.desktopBridgeAppLaunched) { - this.currentConnectionIndex = Int32.Parse(sender.AppServiceName); + lock (thisLock) + { + this.currentConnectionIndex = Int32.Parse(sender.AppServiceName); + } this.desktopBridgeConnection = desktopBridgeConnections[this.currentConnectionIndex]; // Send message to the desktopBridge component and wait for response