System.Threading.ThreadAbortException:线程被终止-哪个线程
本文关键字:线程 终止 ThreadAbortException System Threading | 更新日期: 2023-09-27 18:19:20
我有一个网站,它不断地从另一个网站读取数据,并在地图上绘制信息。我有一个套接字以
开头client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
ReceiveCallback读取数据并将其转储到Queue中供另一个线程分析,然后设置等待句柄以启动该线程。
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
设置等待句柄
// 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());
}
}
你会看到我已经尽力抓住每一个我能想到的可能性。它经常在读取零字节时失败,然后重新启动。(我检查看到重启过程开始时没有进行)
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");
}
您超时了。增加ExecutionTimeout,因为它适用于我和其他人使用AsyncCallback
并收到您发布的这个非常错误。
<compilation debug="false"></compilation>
<httpRuntime executionTimeout="1800"/> //1800 seconds - 30 minutes.
下面是在哪里进行上述更改的演练,下面是与您的情况类似的解决方案。