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 its constructor and only serves to send the packets.
It looks something like:
public class A{
private UdpClient m_Client;
public class A(string host, int port){
m_Client = new UdpClient(host, port);
}
public void Send(string dataToSend){
var data= encoding.GetBytes(dataToSend);
client.BeginSend(data, data.Length, null, null);
}
}
My questions is:
I know that UdpClient isn't thread-safe (according to MSDN documentation), what is the best way to support multithreaded without using locking mechanism?
On each send create new instance of the UdpClient? (just use some local UdpClient var). performance?
Use ThreadLocal for the UdpClient? but what about the disposing of the UdpClient in this situation?
Any other solution?