c#.net循环线程堆栈溢出

mat*_*wen 1 .net c# multithreading winforms

我正在尝试在后台运行一个任务,检查数据库中的表中的多个记录,如果自上次检查后该数字已更改,请获取这些记录,并对它们进行一些操作.

使用以下代码,我在大约两个小时后收到堆栈溢出.应用程序在此期间不执行任何操作,只是检查,并且没有作业添加到数据库中.

private Thread threadTask = null;
private int recordCount = 0;

private void threadTask_Start()
{
    if (threadTask == null) {
        threadTask = new Thread(taskCheck);
        threadTask.Start();
    }
}

private void taskCheck()
{
     int recordCountNew = GetDBRecordCound();
     if (recordCountNew != recordCount)
     {
         taskDo();
         recordCount = recordCountNew; // Reset the local count for the next loop
     }
     else
         Thread.Sleep(1000); // Give the thread a quick break

     taskCheck();          
}

private void taskDo()
{
    // get the top DB record and handle it
    // delete this record from the db
}
Run Code Online (Sandbox Code Playgroud)

当它溢出时,taskCheck()调用堆栈中有大量的内容.我猜测taskCheck()永远不会完成,直到taskCheck()完成,ergo溢出,因此它们都保留在堆栈中.这显然不是解决这个问题的正确方法,那是什么?

Med*_*Man 9

你得到一个堆栈溢出,因为在taskCheck结束时,你再次调用taskCheck.你永远不会退出函数taskCheck,你最终会越来越多地调用,直到你的堆栈溢出.你应该做的是在taskCheck中使用while循环:

private void taskCheck()
{
   while(true)
   {
       int recordCountNew = GetDBRecordCound();
       if (recordCountNew != recordCount)
       {
           taskDo();
           recordCount = recordCountNew; // Reset the local count for the next loop
       }
       else
           Thread.Sleep(1000); // Give the thread a quick break
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 他还应该有一个try {} catch(ThreadAbortException){} finally {}块. (3认同)