问题
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