WSACancelBlockingCall exception

后端 未结 5 1065
生来不讨喜
生来不讨喜 2020-12-24 00:51

Ok, I have a strange exception thrown from my code that\'s been bothering me for ages.

System.Net.Sockets.SocketException: A blocking operation was interrupt         


        
5条回答
  •  醉话见心
    2020-12-24 01:36

    This could happen on a serverSocket.Stop(). Which I called whenever Dispose was called.

    Here is how my exception handling for the listen thread looked like:

    try
    {
        //...
    }
    catch (SocketException socketEx)
    {    
        if (_disposed)
            ar.SetAsCompleted(null, false); //exception because listener stopped (disposed), ignore exception
        else
            ar.SetAsCompleted(socketEx, false);
    }
    

    Now what happened was, every so often the exception would occur before _disposed was set to true. So the solution for me was to make everything thread safe.

提交回复
热议问题