G
Guest
I'm new to C# and threading, so hopefully this is a simple newbie question.
I have a form that is supposed to listen for network traffic on a given port
and decode and display any interesting traffic it sees. To do this I've
launched a separate thread to do the listening.
When I run my form it seems to work fine, but when I close the form the
thread is not being terminated and Task Manager shows the application process
still executing. I tried adding code the the form closing event to abort the
thread, then join it to make sure it was closing properly, and the
application stopped on the join() call, reinforcing that the thread was not
terminating.
Please take pity on a newbie and tell me what I'm I'm doing wrong, or point
me to a good reference. The examples I've seen do no cleanup of the thread,
so I assumed it is supposed to be terminated with the parent application, but
that does not seem to be the case.
The interesting code is:
private void frmMain_Load(object sender, System.EventArgs e)
{
this._ServerIP = new IPEndPoint(IPAddress.Parse
App.Config.DestinationIPAddress), App.Config.DestinationPort);
this._Client = new UdpClient(new IPEndPoint(IPAddress.Any,
App.Config.ListenPort));
this._ListenThread = new Thread(new ThreadStart(RecieveBroadcast));
this._ListenThread.Start();
}
private void RecieveBroadcast()
{
IPEndPoint recieveIP = new IPEndPoint(IPAddress.Any, 0);
while(true)
{
byte[] data = _Client.Receive(ref recieveIP);
}
}
I have a form that is supposed to listen for network traffic on a given port
and decode and display any interesting traffic it sees. To do this I've
launched a separate thread to do the listening.
When I run my form it seems to work fine, but when I close the form the
thread is not being terminated and Task Manager shows the application process
still executing. I tried adding code the the form closing event to abort the
thread, then join it to make sure it was closing properly, and the
application stopped on the join() call, reinforcing that the thread was not
terminating.
Please take pity on a newbie and tell me what I'm I'm doing wrong, or point
me to a good reference. The examples I've seen do no cleanup of the thread,
so I assumed it is supposed to be terminated with the parent application, but
that does not seem to be the case.
The interesting code is:
private void frmMain_Load(object sender, System.EventArgs e)
{
this._ServerIP = new IPEndPoint(IPAddress.Parse
App.Config.DestinationIPAddress), App.Config.DestinationPort);
this._Client = new UdpClient(new IPEndPoint(IPAddress.Any,
App.Config.ListenPort));
this._ListenThread = new Thread(new ThreadStart(RecieveBroadcast));
this._ListenThread.Start();
}
private void RecieveBroadcast()
{
IPEndPoint recieveIP = new IPEndPoint(IPAddress.Any, 0);
while(true)
{
byte[] data = _Client.Receive(ref recieveIP);
}
}