本文介绍了多任务控制台应用程序上的stackoverflow异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序运行了很长的算法,所以我在许多任务中都使用它.当Job方法在任务中工作时,我还有另一种方法来检查所有任务是否都已完成或某些任务是否仍在工作.但是JobChecker方法给了我stackoverflow异常.为什么会给出错误,我该如何解决?

I have a probram which runs some long algorithim, so I work it in many tasks.while Job methods working in tasks I have another method which checking if all tasks done or if some still working. but JobChecker method gave me stackoverflow exception. why It gives error and how can I fix this?

public void Main()
{
    while ((line = sr.ReadLine()) != null)
    {
         string lineTemp = line;
         taskList.Add(Task.Run(() => Job(lineTemp, last)));
    }

    JobChecker(taskList);
}

public void JobChecker(List<Task> taskList)
{
    do
    {
        System.Threading.Thread.Sleep(1000);
    }
    while (taskList.Any(x => x.Status == TaskStatus.Running));
}

推荐答案

使用忙碌的等待功能,而不要使用Task.WaitAllTask.WhenAll

Instead of busy waiting, use the built-in functions like Task.WaitAll or Task.WhenAll

这篇关于多任务控制台应用程序上的stackoverflow异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 20:55