C# .Net receiving UDp packets in separater thread and application exit(C# .Net 在分离线程和应用程序退出中接收 UDp 数据包)
问题描述
In my Winforms application, I am receiving data over UDP in a dedicated thread and raising an event every time a new data packet has been received. The application works fine, but the problem is that in the application exit, the UDP listener keeps listening and thus keeps the app running.
Threading is not very familiar topic to me, so my question is what is the correct way to close the UDP listening thread? Also, I would like to understand why doesnt the UDP listener exit the while loop when I call the StopListener() from the main thread.
My UDP listener looks like this:
class UDPListener
{
private int m_portToListen = 2003;
private volatile bool listening;
Thread m_ListeningThread;
public event EventHandler<MyMessageArgs> NewMessageReceived;
//constructor
public UDPListener()
{
this.listening = false;
}
public void StartListener(int exceptedMessageLength)
{
if (!this.listening)
{
m_ListeningThread = new Thread(ListenForUDPPackages);
this.listening = true;
m_ListeningThread.Start();
}
}
public void StopListener()
{
this.listening = false;
}
public void ListenForUDPPackages()
{
UdpClient listener = null;
try
{
listener = new UdpClient(m_portToListen);
}
catch (SocketException)
{
//do nothing
}
if (listener != null)
{
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, m_portToListen);
try
{
while (this.listening)
{
Console.WriteLine("Waiting for UDP broadcast to port " +m_portToListen);
byte[] bytes = listener.Receive(ref groupEP);
//raise event
NewMessageReceived(this, new MyMessageArgs(bytes));
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
listener.Close();
Console.WriteLine("Done listening for UDP broadcast");
}
}
}
}
public class MyMessageArgs : EventArgs
{
public byte[] data { get; set; }
public MyMessageArgs(byte[] newData)
{
data = newData;
}
}
And in the MainWindow_FormClosing() event (in the main UI thread) I do the following:
m_udpListener.StopListener();
this.m_udpListener.NewMessageReceived -= OnNewMessageReceived;
The way the app works now is after application exit, the application is left hanging, but the next received UDP packet raises an exception in the UDP listener, and then the finally block gets executed, but not before.
Clearly I have misunderstood something, help would be appreciated!
Your code looks OK but you can add a few lines to make Closing more reliable.
public void StopListener()
{
this.listening = false;
listener .Close(); // forcibly end communication
}
Stopping requires a little time, so call this as early as possible (ie from the Close button and not from Window_Closed event) :
// first disconnect the event
this.m_udpListener.NewMessageReceived -= OnNewMessageReceived;
m_udpListener.StopListener();
Application.DoEvents(); // allow processing of outstanding packets
And although it should not critical, I would make the Thread a background one:
m_ListeningThread = new Thread(ListenForUDPPackages);
m_ListeningThread.IsBackground = true;
这篇关于C# .Net 在分离线程和应用程序退出中接收 UDp 数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# .Net 在分离线程和应用程序退出中接收 UDp 数据包


基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01