我有一些代码启动一些线程让它们执行,然后使用while循环来检查当前时间是否超过了设置的超时时间,或者是否处理了正确数量的结果(通过检查int在类对象中)(Thread.Sleep()
在循环之间等待)为什么我的代码停止并且没有返回异常?
一旦将while循环设置为退出,它将在线程上调用Abort()
并将数据返回给调用方法的函数。
在调试代码时,我发现在单独的线程上运行的代码中可能存在异常,在某些情况下,我会适当地处理这些异常,有时候我不想做任何特定的操作。
我所看到的是我的代码进入while循环并且线程休眠,然后没有任何东西从我的函数返回,无论是数据还是异常。代码执行完全停止。
任何想法可能发生什么?
代码示例:
System.Threading.Thread sendThread =
new System.Threading.Thread(new System.Threading.ThreadStart(Send));
sendThread.Start();
System.Threading.Thread receiveThread =
new System.Threading.Thread(new System.Threading.ThreadStart(Receive));
receiveThread.Start();
// timeout
Int32 maxSecondsToProcess = this.searchTotalCount * timeout;
DateTime timeoutTime = DateTime.Now.AddSeconds(maxSecondsToProcess);
Log("Submit() Timeout time: " + timeoutTime.ToString("yyyyMMdd HHmmss"));
// while we're still waiting to receive results & haven't hit the timeout,
// keep the threads going
while (resultInfos.Count < this.searchTotalCount && DateTime.Now < timeoutTime)
{
Log("Submit() Waiting...");
System.Threading.Thread.Sleep(10 * 1000); // 1 minute
}
Log("Submit() Aborting threads"); // <== this log doesn't show up
sendThread.Abort();
receiveThread.Abort();
return new List<ResultInfo>(this.resultInfos.Values);
你不应该使用Thread.Abort,做一个谷歌的原因为什么。 – 2010-05-11 14:03:37
10 * 1000不是一分钟:)(1000 * 60毫秒= 60秒= 1分钟) – Simon 2010-05-11 14:08:17
是的,我知道:)我改变了这个测试的目的,但从未更新评论.... – BeckyLou 2010-05-11 14:14:55