I have thread with while(true) which maintain my connection with client. Inside I have if with bool. When I click button it set "true" and file transfering is started (all instruction is in if).
My question is, how I could maintain connection in thread without while, or how I should go out that "while" when my client is disconnected.
public void SayHi()
{
try
{
while (true)//for maintain connection
{
if(form1.transfer)
{
NetworkStream networkStream = clientSocket.GetStream();
CardNumber++;
form1.SendQuestion(clientSocket, CardNumber, Convert.ToInt32(clNo));
form1.transfer = false;
Thread AnswerThread = new Thread(WaitForAnswer);
AnswerThread.IsBackground = true;
AnswerThread.Start();
}
}
}