I'm making a multithreaded chat server and chat client. The client has a Form1 called Login and a Form2 called MainProgram. The following code is from "Login". What I'm trying to do is transitioning from Login to MainProgram...
MainProgram mP = new MainProgram(clientSocket, username);
mP.Closed += (s, args) => this.Close();
this.Hide();
mP.ShowDialog();
mP.Show();
... however. When assigning mP MainProgram mP = new MainProgram(clientSocket, username);
The code get's stuck in the thread specified here:
public MainProgram(TcpClient c, string u)
{
InitializeComponent();
try
{
serverStream = c.GetStream();
clientSocket = c;
username = u;
new Thread(Receive()) { IsBackground = true }.Start();
}
Here is the Thread:
private ThreadStart Receive()
{
while (true)
{
try
{
byte[] inStream = new byte[1024];
serverStream.Read(inStream, 0, inStream.Length);
string returndata = Encoding.ASCII.GetString(inStream);
returndata = returndata.Substring(0, returndata.IndexOf("$"));
Msg($"{returndata}");
}
catch(Exception e)
{
MessageBox.Show($"{e.Message}\n\n{e.ToString()}");
}
}
}
Please note that the thread is supposed to be running this while loop indefinitely, but in the background. Right now it doesn't make a new thread and runs on the MainThread. The problem is that i don't know how to start this thread without making the client get stuck in this while loop. And not transitioning.