小编Bec*_*Lou的帖子

为什么我的代码停止而没有返回异常?

我有一些代码启动了几个线程让它们执行,然后使用while循环检查当前时间是否超过设置的超时时间,或者是否已经处理了正确的结果数(通过检查class object)(用a Thread.Sleep()来循环之间等待)

一旦while循环设置为exit,它就会调用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
} …
Run Code Online (Sandbox Code Playgroud)

c# multithreading exception

8
推荐指数
2
解决办法
211
查看次数

标签 统计

c# ×1

exception ×1

multithreading ×1