From 73dc0b94def493d998d47c090b42ac290e294d1f Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 20 Oct 2020 18:27:53 +0200 Subject: [PATCH] [EDIT] made main window open only when the client has connected --- Client/Client.cs | 2 ++ Client/Views/LoginScreen.xaml.cs | 17 +++++++++++------ SharedClientServer/ClientServerUtil.cs | 1 + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index d6e1220..6ba9b16 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -17,6 +17,7 @@ namespace Client public int Port = 5555; public bool Connected = false; private string username; + public Callback OnSuccessfullConnect; public Client(string username) { @@ -31,6 +32,7 @@ namespace Client Debug.Write("finished connecting to server"); this.tcpClient.EndConnect(ar); this.stream = tcpClient.GetStream(); + OnSuccessfullConnect?.Invoke(); SendMessage(JSONConvert.ConstructUsernameMessage(username)); this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); } diff --git a/Client/Views/LoginScreen.xaml.cs b/Client/Views/LoginScreen.xaml.cs index 7dfe537..780b619 100644 --- a/Client/Views/LoginScreen.xaml.cs +++ b/Client/Views/LoginScreen.xaml.cs @@ -28,13 +28,18 @@ namespace Client.Views { User user = new User(usernameTextbox.Text); Client client = new Client(user.Username); + client.OnSuccessfullConnect = () => + { + // because we need to start the main window on a UI thread, we need to let the dispatcher handle it, which will execute the code on the ui thread + Application.Current.Dispatcher.Invoke(delegate { + data.User = user; + data.Client = client; + MainWindow startWindow = new MainWindow(); + startWindow.Show(); + this.Close(); + }); + }; - data.User = user; - data.Client = client; - - MainWindow startWindow = new MainWindow(); - startWindow.Show(); - this.Close(); } } } diff --git a/SharedClientServer/ClientServerUtil.cs b/SharedClientServer/ClientServerUtil.cs index 3572562..f79c448 100644 --- a/SharedClientServer/ClientServerUtil.cs +++ b/SharedClientServer/ClientServerUtil.cs @@ -6,6 +6,7 @@ using System.Text; namespace SharedClientServer { + public delegate void Callback(); class ClientServerUtil { // creates a message array to send to the server or to clients