use .Net UdpClient in a multithreaded environment

前端 未结 2 1048
一生所求
一生所求 2021-01-06 10:27

I have an instance of a class (lets call it A) which serves some threads, this instance only sends UDP packets via the UdpClient class. It initialize the the UdpClient in it

相关标签:
2条回答
  • 2021-01-06 11:02

    I think the best way here would be to create a new UdpClient every time the method is called. This way, you can be sure the code is safe. The performance isn't likely to be a problem, and if profiling shows that it is, only then you should start solving that.

    Also, you shouldn't forget to call EndSend() after each BeginSend() (ideally in the callback).

    0 讨论(0)
  • 2021-01-06 11:06

    Eventually I believe that my current implementation should work with no issues (as long as Microsoft won't change the UdpClient class implementation).

    For whom it might be interesting: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cbdd9818-00f0-499f-a935-d8e555899d64

    0 讨论(0)
提交回复
热议问题