private async void MainPage_Unloaded(object sender, object args) { try { // Cleanup await streamingService.CleanupAsync(); httpCameraServer.Dispose(); connection.Dispose(); socket.Dispose(); motors.Dispose(); led?.Dispose(); distanceSensor.Dispose(); } catch { } }
/// <summary> /// The async callback for <see cref="ServerHost.BeginAcceptingTcpClient(TcpListener, bool)" /> method. /// </summary> /// <param name="ar">The async result.</param> protected void EndAcceptingTcpClient(IAsyncResult ar) { var waitForNext = true; ConnectionWithClient clientConn = null; TcpListener listener = null; RemoteConnection remoteConn = null; Action disposeConnections = () => { if (clientConn != null) { try { clientConn.Dispose(); } catch (ObjectDisposedException) { // ignore } catch (Exception ex2) { this.RaiseError(ex2); } } else if (remoteConn != null) { try { remoteConn.Dispose(); } catch (ObjectDisposedException) { // ignore } catch (Exception ex2) { this.RaiseError(ex2); } } }; try { listener = ar.AsyncState as TcpListener; if (listener == null) { return; } var client = listener.EndAcceptTcpClient(ar); remoteConn = new RemoteConnection(this.Application, client.Client, true); clientConn = new ConnectionWithClient(this, remoteConn); clientConn.Closed += this.ConnectionWithClient_Closed; clientConn.Disposed += this.ConnectionWithClient_Disposed; this.InvokeForConnections((list, state) => list.Add(state.NewConnection), new { NewConnection = clientConn, }); if (this.StartCommunicationWithClient(clientConn).IsFalse()) { disposeConnections(); } } catch (ObjectDisposedException) { waitForNext = false; } catch (Exception ex) { disposeConnections(); this.RaiseError(ex); } finally { if (waitForNext) { this.BeginAcceptingTcpClient(listener); } } }