Ejemplo n.º 1
0
 private async void startButton_Click(object sender, EventArgs e)
 {
     connection = new ServerConnection();
     connection.Start();
     if (connection.IsConnected)
     {
         LobbyDialog lobbyDialog = new LobbyDialog(connection);
         lobbyDialog.Owner = this;
         Task <int> lobbyTask = connection.QueryLobby(lobbyDialog);
         lobbyDialog.ShowDialog();
         await lobbyTask;
         lobbyDialog.Close();
         if (lobbyTask.Result == 0)
         {
             this.menuPanel.Visible        = false;
             this.constructorPanel.Visible = true;
             connection.BackgroundLoop();
             listenOpenPanel();
             timerOfConstructionPhase.Start();
         }
     }
 }
Ejemplo n.º 2
0
        public Task <int> QueryLobby(LobbyDialog lobbyDialog)
        {
            cancelTokenSource = new CancellationTokenSource();
            var token = cancelTokenSource.Token;

            return(Task.Run(() => {
                int result = -1;
                while (true)
                {
                    if (!this.IsConnected)
                    {
                        lobbyDialog.Invoke(new Action(() => {
                            lobbyDialog.Close();
                        }));

                        break;
                    }
                    send("IsLobbyReady");
                    if (read() == "Yes")
                    {
                        result = 0;
                        lobbyDialog.Invoke(new Action(() => {
                            lobbyDialog.Close();
                        }));
                        break;
                    }
                    if (token.IsCancellationRequested)
                    {
                        serverConnection.Close();
                        break;
                    }
                    Thread.Sleep(100);
                }
                return result;
            }, token));
        }