[EDIT] made main window open only when the client has connected

This commit is contained in:
Sem van der Hoeven
2020-10-20 18:27:53 +02:00
parent f6c70bc717
commit 73dc0b94de
3 changed files with 14 additions and 6 deletions

View File

@@ -17,6 +17,7 @@ namespace Client
public int Port = 5555; public int Port = 5555;
public bool Connected = false; public bool Connected = false;
private string username; private string username;
public Callback OnSuccessfullConnect;
public Client(string username) public Client(string username)
{ {
@@ -31,6 +32,7 @@ namespace Client
Debug.Write("finished connecting to server"); Debug.Write("finished connecting to server");
this.tcpClient.EndConnect(ar); this.tcpClient.EndConnect(ar);
this.stream = tcpClient.GetStream(); this.stream = tcpClient.GetStream();
OnSuccessfullConnect?.Invoke();
SendMessage(JSONConvert.ConstructUsernameMessage(username)); SendMessage(JSONConvert.ConstructUsernameMessage(username));
this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null); this.stream.BeginRead(buffer, 0, buffer.Length, new AsyncCallback(OnReadComplete),null);
} }

View File

@@ -28,13 +28,18 @@ namespace Client.Views
{ {
User user = new User(usernameTextbox.Text); User user = new User(usernameTextbox.Text);
Client client = new Client(user.Username); 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();
} }
} }
} }

View File

@@ -6,6 +6,7 @@ using System.Text;
namespace SharedClientServer namespace SharedClientServer
{ {
public delegate void Callback();
class ClientServerUtil class ClientServerUtil
{ {
// creates a message array to send to the server or to clients // creates a message array to send to the server or to clients