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?
Loading
Sunny ChenPosted Jul 9, 2008, 8:48 PM
1. Declare your shut variable as volatile
2. Shutdown your Socket connection after the thread terminates:
shut=1;
th.Join();
ss.Close();
Posted Jul 8, 2008, 6:34 PM
aggil antonyPosted Jul 8, 2008, 2:49 PM
Posted Jul 8, 2008, 2:07 PM