WSACancelBlockingCall exception

后端 未结 5 1025
生来不讨喜
生来不讨喜 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:14

    This is my example solution to avoid WSAcancelblablabla: Define your thread as global then you can use invoke method like this:

    private void closinginvoker(string dummy)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<string>(closinginvoker), new object[] { dummy });
                return;
            }
            t_listen.Abort();
            client_flag = true;
            c_idle.Close();
            listener1.Stop();
        }
    

    After you invoke it, close the thread first then the forever loop flag so it block further waiting (if you have it), then close tcpclient then stop the listener.

    0 讨论(0)
  • 2020-12-24 01:23

    Is it possible that the serverSocket is being closed from another thread? That will cause this exception.

    0 讨论(0)
  • 2020-12-24 01:23

    Same here! But i figured out, that the ReceiveBuffer on 'server-side' was flooded from the clients! (In my case a bunch of RFID-Scanners, who kept spamming the TagCode, instead of stop sending until next TagCode arrives)

    It helped to raise the ReceiveBuffers and reconfigure the scanners...

    0 讨论(0)
  • 2020-12-24 01:25

    More recently I saw this exception when using HttpWebRequest to PUT a large file and the Timeout period was passed.

    Using the following code as long as your upload time > 3 seconds it will cause this error as far as I could see.

    string path = "Reasonably large file.dat";
    int bufferSize = 1024;
    byte[] buffer = new byte[bufferSize];
    System.Net.HttpWebRequest req = (HttpWebRequest)System.Net.HttpWebRequest.Create("Some URL");
    req.Method = "PUT";
    req.Timeout = 3000; //3 seconds, small timeout to demonstrate
    long length = new System.IO.FileInfo(path).Length;
    using (FileStream input = File.OpenRead(path))
    {
        using (Stream output = req.GetRequestStream())
        {
            long remaining = length;
            int bytesRead = 0;
            while ((bytesRead = input.Read(buffer, 0, (int)Math.Min(remaining, (decimal)bufferSize))) > 0)
            {
                output.Write(buffer, 0, bytesRead);
                remaining -= bytesRead;
            }
            output.Close();
        }
    input.Close();
    }
    
    0 讨论(0)
  • 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.

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