private void tlsbExit_Click(object sender, EventArgs e) { //Stop trying to receive datas ConnectionTimer.Stop(); //cancel server task this.client.sendData(new Packet(Station, PacketType.CloseConnection, 1)); this.client.Disconnect(); if (server != null) { if (server.IsRunning && !cts.IsCancellationRequested) { server.SendData(new Packet(Sender.Server, PacketType.CloseConnection, 1)); cts.Cancel(); try { runServer.Wait(); } catch (AggregateException ex) { } finally { cts.Dispose(); } } } this.Close(); }
public override void Exit() { HandleLines = false; PingTimer.Stop(); ConnectionTimer.Stop(); base.Exit(); }
private void ConnectionTimer_Tick(object sender, EventArgs e) { var check = CheckForArduinoPort(); if (check == "") { _connected = false; return; } ConnectionTimer.Stop(); _serial = new Serial(check, 9600, Parity.None, 8, StopBits.One); _serial.Open(); _serial.DataReceived += SerialPort_DataReceived; _serial.ErrorReceived += SerialPort_ErrorReceived; _connected = true; }
//Close properly private void Lobby_FormClosing(object sender, FormClosingEventArgs e) { if (this.DialogResult == DialogResult.Cancel) { //Stop trying to receive datas ConnectionTimer.Stop(); //cancel server task if (client != null) { if (this.client.GetConnectionState()) { this.client.sendData(new Packet(Sender.Server, PacketType.CloseConnection, 1)); Thread.Sleep(50); this.client.Disconnect(); } } if (server != null) { if (server.IsRunning && !cts.IsCancellationRequested) { cts.Cancel(); try { runServer.Wait(); } catch (AggregateException ex) { } finally { cts.Dispose(); } } } } //Exited via game exit button if (e.CloseReason == CloseReason.UserClosing) { MessageBox.Show("Lobby exited."); } else { //Stop trying to receive datas ConnectionTimer.Stop(); //cancel server task this.client.sendData(new Packet(Station, PacketType.CloseConnection, 1)); Thread.Sleep(50); this.client.Disconnect(); if (server != null) { if (server.IsRunning && !cts.IsCancellationRequested) { this.server.SendData(new Packet(Sender.Server, PacketType.CloseConnection, 1)); cts.Cancel(); try { runServer.Wait(); } catch (AggregateException ex) { } finally { cts.Dispose(); } } } } }