stackoverflow exception on multitasking console app

99封情书 提交于 2019-12-12 03:19:10

问题


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));
}

回答1:


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



来源:https://stackoverflow.com/questions/31442789/stackoverflow-exception-on-multitasking-console-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!