I've made a program that has a thread that continuously receives the bytes from a server via socket.
The code is like this:
data = new byte[1024];
receiverThread = new Thread(new ThreadStart(Rec));
receiverThread.Start();
public void Rec()
{
while(true)
{
try
{
recv = client.ReceiveFrom(data, ref remoteEP);
txtDisplay.Text += Encoding.ASCII.GetString(data, 0, recv) + "\r\n" ;
/*txtDisplay is the Textbox in which the received strings are displayed */
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
break;
}
}
}
Now the problem is even when I close the main form, the program doesn't terminate. The program gets stuck in the code i've marked with the red.
So please can anybody can tell me, what can I do to completely terminate the program

Program doesn't Terminate
Feras
Did you close the socket This should force the ReceiveFrom() method to return allowing the thread to terminate (yes, you need to terminate the thread first). E.g:
receiverThread.Abort();
client.Close();
Well, it worked for me anyway.
GoffQ
Maxon
You can use this method in and form closing event
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
environment.exit(0);
}
this might not be the effective one to implement but it can be helpful to you
gorovvv
Try receiverThread.Interrupt() instead.
ChuckH
if (recv == string.Empty)
break;
You need something to break out of the while loop.
tris111
System.Diagnostics.
Process process = System.Diagnostics.Process.GetCurrentProcess();process.Kill();
Neo
Scott_Morrison
Heidi8139
That helps.