为什么调试模式不能保证完成任务(.net核心,C#)

问题描述 投票:0回答:1

每个人。

我是这里和编程世界的新手。

MS docs所说的一项任务是可以保证完成的一项工作。我发现在发布版本中是正确的,但在调试版本中则不是。

我的测试代码非常简单。

        static void Main(string[] args)
        {            
            Console.WriteLine("1st Code executed");
            Task task = new Task(() =>
            {
                Console.WriteLine("2nd Code executed");
                Console.WriteLine("3rd Code executed");
            });
            task.Start();
            Console.WriteLine("4th Code executed");

            Console.ReadLine();
           }

在调试模式下,它导致以下三个之一:

         /*Mostly
         * 1st Code executed
         * 4th Code executed

         * rarely
         * 1st Code executed
         * 4th Code executed
         * 2nd Code executed

         * rarely
         * 1st Code executed
         * 4th Code executed
         * 2nd Code executed
         * 3rd Code executed */

在发行版中,没有例外,尽管任务中的第4行和第4行按任意顺序排列,但在应用程序完成之前都显示了所有四行。

我的问题是,使应用程序完成而不以调试模式完成任务的原因是什么,此外,我如何通过调试选项来修复它(如果有的话。)>

每个。我是这里和编程世界的新手。 MS文档说任务是一项可以保证完成的工作。我发现在发布版本中确实如此,但在调试版本中却并非如此。我的...

c# .net debugging task
1个回答
0
投票

快速解答

© www.soinside.com 2019 - 2024. All rights reserved.