private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { try { if ((sender.TriggerDetails as AppServiceTriggerDetails)?.AppServiceConnection is AppServiceConnection DisConnection) { try { DisConnection.RequestReceived -= Connection_RequestReceived; if (PairedConnections.TryRemove(DisConnection, out AppServiceConnection ServerConnection)) { Task.WaitAny(ServerConnection.SendMessageAsync(new ValueSet { { "ExecuteType", "Execute_Exit" } }).AsTask(), Task.Delay(2000)); } } finally { DisConnection.Dispose(); } } } catch (Exception ex) { Debug.WriteLine($"Error was threw in CommunicateService: {ex.Message}"); } finally { Deferral.Complete(); } }
private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { lock (Locker) { try { if ((sender.TriggerDetails as AppServiceTriggerDetails)?.AppServiceConnection is AppServiceConnection DisConnection) { DisConnection.RequestReceived -= Connection_RequestReceived; if (ClientConnections.ContainsKey(DisConnection)) { ServerConnection?.SendMessageAsync(new ValueSet { { "ExcuteType", "Excute_RequestClosePipe" }, { "Guid", ClientConnections[DisConnection] } }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); ClientConnections.Remove(DisConnection); DisConnection.Dispose(); if (ClientConnections.Count == 0) { ServerConnection?.SendMessageAsync(new ValueSet { { "ExcuteType", "Excute_Exit" } }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); } } else { if (ReferenceEquals(DisConnection, ServerConnection)) { ServerConnection.Dispose(); ServerConnection = null; } else { DisConnection.Dispose(); if (ClientConnections.Count == 0) { ServerConnection?.SendMessageAsync(new ValueSet { { "ExcuteType", "Excute_Exit" } }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); } } } } } catch (Exception ex) { Debug.WriteLine($"Error thrown in CommuniteService: {ex.Message}"); } finally { Deferral.Complete(); } } }
private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason) { lock (Locker) { try { if ((sender.TriggerDetails as AppServiceTriggerDetails)?.AppServiceConnection is AppServiceConnection DisConnection) { try { DisConnection.RequestReceived -= Connection_RequestReceived; if (ServiceAndClientConnections.FirstOrDefault((Pair) => Pair.Server == DisConnection || Pair.Client == DisConnection) is ServerAndClientPair ConnectionPair) { if (ConnectionPair.Server == DisConnection) { ConnectionPair.Server = null; } else { ServiceAndClientConnections.Remove(ConnectionPair); ConnectionPair.Client = null; ConnectionPair.Server?.SendMessageAsync(new ValueSet { { "ExecuteType", "Execute_Exit" } }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult(); } } } finally { DisConnection.Dispose(); } } } catch (Exception ex) { Debug.WriteLine($"Error thrown in CommuniteService: {ex.Message}"); } finally { Deferral.Complete(); } } }