private bool Connect(string hostname, int port, string username, string password, bool reconnect) { const int DialogHiddenTime = 1000; // If we are reconnecting, try to reconnect 5 times. Otherwise, only try once int retryCount = reconnect ? 5 : 0; this.hostname = hostname; this.port = port; this.username = username; this.password = password; if (reconnect) { dialog = new ConnectionDialog("Connection Lost", "Trying to reconnect..."); } else { dialog = new ConnectionDialog("Infinichat", "Connecting to Infinichat..."); } dialog.Owner = App.Current.MainWindow; /* Create a new thread which connects to the given hostname and port and then logs in * with the given username and password. The connection thread will set the connectDone * event when these operations complete. If they complete quickly, we don't want to show * a dialog. */ var thread = new Thread(new ParameterizedThreadStart(ConnectThread)); thread.Name = "Connection Thread"; thread.Start(retryCount); // For user experience reasons, only show the dialog if connecting is taking a long time if (!connectDone.WaitOne(DialogHiddenTime)) { dialog.ShowDialog(); lock (dialogLock) { dialog = null; } connectDone.Set(); thread.Join(); } else { dialog.ShowDialog(); lock (dialogLock) { dialog = null; } } connectDone.Reset(); if (!connectionSucceeded) { System.Windows.MessageBox.Show(App.Current.MainWindow, connectionErrorMessage, "Error Logging In"); } return(connectionSucceeded); }
private void UI_Bttn_Connect_Click(object sender, EventArgs e) { if (!_disconnect) { dlg = new ConnectionDialog(); dlg._socketPass = new ConnectionDialog.delVoidSocket(ConnectResult); if (dlg.ShowDialog() == DialogResult.OK) { // Change connect to disconnect UI_Bttn_Connect.Text = "Disconnect"; _disconnect = true; // Enable gameplay UI_Bttn_Guess.Enabled = true; dlg = null; } } else { DisconnectButton(); } }