How to stop thread??

Jul 8 2008 10:13 AM
I'm doing a Network project . I used a function to listen to clients and maked it as a thread. The main problem is when I closed the application,the main window closes and the application runs in background.I cannot close the application Even from the Task Manager.
Code used
private void MainForm_Load(object sender, EventArgs e)
{
th = new Thread(new ThreadStart(listen));
th.Start();

}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{

shut=1;
th.Abort();
}



public void listen()
{
ss.Bind(new IPEndPoint(IPAddress.Any, 4000));
ss.Listen(-1);

while (shut==0)
{
cs = ss.Accept();
c1 = 0;
mysocket[i] = cs;
i++;
clientsock = cs;
ThreadPool.QueueUserWorkItem(new WaitCallback(listensend), clientsock);
}
}


Also the socket string used is
Socket ss = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);


Can I close the Application forcibly?

Answers (4)