I have two UDP clients:
UdpClient _udpReceiver
and
UdpClient _udpSender
For receiving a message I'm using _udpReceiver.BeginReceive(...);
And for sending: _udpSender.BeginSend(data, data.Length, new AsyncCallback(StopCallback, _udpSender);
What I'm trying to achieve is to stop any sending and receiving and somehow unbind the ports which _udpReceiver and _udpSender are using. But with no success:
private void StopCallback(IAsyncResult ar)
{
try
{
UdpClient client = (UdpClient)ar.AsyncState;
client.EndSend(ar);
client.Close(); //app crashes here
_udpSender.Client.Close(); //and here
_udpReceiver.Client.Close(); //and here too
}
catch (ObjectDisposedException ex)
{
//and this one is never getting caught
}
}
UPD: attached link to a project which reproduces the crash