void Listen() { Thread listenThread = new Thread(() => { try { SocketData data = (SocketData)socket.Receive(); ProcessData(data); } catch (Exception e) { } }); listenThread.IsBackground = true; listenThread.Start(); }
void Listen() { //Tránh bị lỗi khi 1 bên thoát đột ngột try { Thread listenThread = new Thread(() => { SocketData data = (SocketData)socket.Receive(); ProcessData(data); }); listenThread.IsBackground = true; listenThread.Start(); } catch { } }
private void ProcessData(SocketData data) { switch (data.Command) { case (int)SocketCommand.NOTIFY: MessageBox.Show(data.Message); break; case (int)SocketCommand.NEW_GAME: this.Invoke((MethodInvoker)(() => { NewGame(); pnlChessBoard.Enabled = false; })); break; case (int)SocketCommand.QUIT: tmCoolDown.Stop(); MessageBox.Show("Người chơi đã thoát!"); caroChess.Ready = false; break; case (int)SocketCommand.SEND_POINT: this.Invoke((MethodInvoker)(() => { prcbCoolDown.Value = 0; tmCoolDown.Start(); OtherPlayerMark(data.Point); })); break; case (int)SocketCommand.END_GAME: break; default: break; } Listen(); }
private void ProcessData(SocketData data) { switch (data.Command) { case (int)SocketCommand.NOTIFY: MessageBox.Show(data.Message); break; case (int)SocketCommand.NEW_GAME: break; case (int)SocketCommand.SEND_POINT: this.Invoke((MethodInvoker)(() => { prcbCoolDown.Value = 0; pnlChessBoard.Enabled = true; tmCoolDown.Start(); ChessBoard.OtherPlayerMark(data.Point); })); break; case (int)SocketCommand.UNDO: break; case (int)SocketCommand.END_GAME: break; case (int)SocketCommand.QUIT: break; default: break; } Listen(); }
private void ProcessData(SocketData data) { switch (data.Command) { case (int)SocketCommand.NOTIFY: MessageBox.Show(data.Message); break; case (int)SocketCommand.NEW_GAME: break; case (int)SocketCommand.SEND_POINT: break; case (int)SocketCommand.QUIT: break; case (int)SocketCommand.UNDO: break; default: break; } }
private void ProcessData(SocketData data) { switch (data.Command) { case (int)SocketCommand.NOTIFY: MessageBox.Show(data.Message); break; case (int)SocketCommand.NEW_GAME: this.Invoke((MethodInvoker)(() => { NewGame(); pnlChessBoard.Enabled = false; })); break; case (int)SocketCommand.SEND_POINT: this.Invoke((MethodInvoker)(() => { prcbCoolDown.Value = 0; pnlChessBoard.Enabled = true; tmCoolDown.Start(); ChessBoard.OtherPlayerMark(data.Point); undoToolStripMenuItem.Enabled = true; })); break; case (int)SocketCommand.UNDO: Undo(); prcbCoolDown.Value = 0; break; case (int)SocketCommand.REDO: Redo(); prcbCoolDown.Value = 0; break; case (int)SocketCommand.END_GAME: this.Invoke((MethodInvoker)(() => { //newGameToolStripMenuItem.Enabled = false; txbPlayerName.Text = ChessBoard.PlayerName.Text; MessageBox.Show(txbPlayerName.Text + " thắng!"); })); break; case (int)SocketCommand.TIME_OUT: MessageBox.Show("Hết giờ"); break; case (int)SocketCommand.QUIT: tmCoolDown.Stop(); MessageBox.Show("Người chơi đã thoát"); break; default: break; } Listen(); }
private void ProcessData(SocketData data) { switch (data.Command) { case (int)SocketCommand.NOTIFY: MessageBox.Show(data.Message); break; case (int)SocketCommand.NEW_GAME: this.Invoke((MethodInvoker)(() => { NewGame(); pnlChessBoard.Enabled = false; })); break; case (int)SocketCommand.SEND_POINT: //vì timer chạy thread khác với main nên phải đưa vào invoke this.Invoke((MethodInvoker)(() => { prcbCoolDown.Value = 0; pnlChessBoard.Enabled = true; tmCoolDown.Start(); ChessBoard.OtherPlayerMark(data.Point); undoToolStripMenuItem.Enabled = true; })); break; case (int)SocketCommand.UNDO: this.Invoke((MethodInvoker)(() => { Undo(); prcbCoolDown.Value = 0; })); break; case (int)SocketCommand.END_GAME: this.Invoke((MethodInvoker)(() => { if (ChessBoard.NumberPlayerWin == 0) { MessageBox.Show("Người chơi 1 thắng"); } else if (ChessBoard.NumberPlayerWin == 1) { MessageBox.Show("Người chơi 2 thắng"); } })); break; case (int)SocketCommand.TIME_OUT: MessageBox.Show("Hết giờ"); break; case (int)SocketCommand.QUIT: this.Invoke((MethodInvoker)(() => { tmCoolDown.Stop(); MessageBox.Show("Người chơi đã thoát"); })); break; default: break; } Listen(); }