System.Threading.ThreadAbortException:线程被中止 - 哪个线程?

时间:2014-08-23 11:48:27

标签: c# multithreading sockets

我有一个网站,它不断从另一个网站读取数据并在地图上绘制该信息。 我有一个套接字

client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReceiveCallback), state);

ReceiveCallback读取数据并将其转储到队列中以供另一个要分析的线程然后进行分析 设置waithandle以启动该线程。

readBytesWaitHandle.Set();

我很少收到以下错误

System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Threading.WaitHandle.WaitOneNative(SafeHandle waitableSafeHandle, UInt32      millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
   at System.Threading.WaitHandle.InternalWaitOne(SafeHandle waitableSafeHandle, Int64 millisecondsTimeout, Boolean hasThreadAffinity, Boolean exitContext)
   at System.Threading.WaitHandle.WaitOne(TimeSpan timeout, Boolean exitContext)
   at WebAPRS.PacketListener.readBytesBuffer() in c:\Users\Alan\Documents\Visual Studio 2013\Projects\WebAPRS\WebAPRS\PacketListener.cs:line 274

问题是 - 哪个线程正在中止?运行ReceiveCallback或等待的那个 ReceiveCallback设置等待句柄。

由于

艾伦

编辑这是ReceiveCallback的代码

// run by the client thread (which is implicitly created)
    private void ReceiveCallback(IAsyncResult ar)
    {
        try
        {
            if (restarting) return;

            // Retrieve the state object and the client socket 
            // from the asynchronous state object.
            timeSpanSinceLastBytes = DateTime.Now - timeSinceLastBytes;
            timeSinceLastBytes = DateTime.Now;
            var state = (StateObject)ar.AsyncState;

            if (!state.workSocket.Connected)
            {
                reStartClient("Error: state.workSocket.Connected =false - Restarting");
                return;
            }

            var client = state.workSocket;

            // Read data from the remote device.
            var bytesRead = client.EndReceive(ar);

            if (bytesRead == 0)
            {
                reStartClient("ERROR: bytes==0 - Restarting");
                return;
            }

            // quickly store the buffer
            storeBytes buff = new storeBytes(state.buffer, bytesRead);
            byteQueue.Enqueue(buff);
            string data = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);
            client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReceiveCallback), state);
            // setoff the readBytes Thread
            readBytesWaitHandle.Set();
        }
        catch(Exception e)
        {
            reStartClient("ReceiveCallback failed : " + e.ToString());
        }
    }

你会看到我试图抓住我能想到的每一个可能性。它经常失败,读取零字节并重新启动OK。 (我检查重启过程是否在开始时没有进行)

catch块没有捕获此错误。 我的网站刚刚死亡(可能每12小时一次)所以我有另一个线程运行检查 每30秒从ReceiveCallback读取输出的线程的运行状况,它打印出上面的错误消息。 代码是:

private void checkRunning(object state)
    {
        if(! ((packetListener.readBytes.ThreadState == ThreadState.Running)||(packetListener.readBytes.ThreadState == ThreadState.WaitSleepJoin)))
        {
            packetListener.appendToDebugFile("readBytes thread not running "+packetListener.readBytes.ThreadState.ToString());
        }
        if(! ((packetListener.readStrings.ThreadState == ThreadState.Running) || (packetListener.readStrings.ThreadState == ThreadState.WaitSleepJoin)))
        {
            packetListener.appendToDebugFile("readStrings thread not running " + packetListener.readStrings.ThreadState.ToString());
        }
        if (packetListener.timeSpanSinceLastBytes > TimeSpan.FromSeconds(30))
        {
            packetListener.appendToDebugFile("bytes not seen for " + packetListener.timeSpanSinceLastBytes.ToString());
        }
        packetListener.appendToDebugFile("Threads running OK");
    }

1 个答案:

答案 0 :(得分:0)

你正在超时。增加ExecutionTimeout,因为它对我和其他人使用AsyncCallback并且收到你发布的这个错误。

<compilation debug="false"></compilation>
<httpRuntime executionTimeout="1800"/> //1800 seconds - 30 minutes. 

以下是where to make the above changes的演练,此处为similar situation to yours getting resolved