在C#中使用任务时不打印 [英] Not print when using a task in C#

查看:100
本文介绍了在C#中使用任务时不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这是一个愚蠢的问题。

Maybe it is a stupid question.

class Program
   {
       static void Main(string[] args)
       {
           Task t = new Task(() =>
           {
               int i = 0;
               for (i = 0; i < 100; i++)
               {
                   Console.WriteLine("*");
               }
           });
           t.Wait();
       }
   }



为什么不打印?


Why it didn't print?

推荐答案

您等待任务完成,但您从未启动任务。使用 t.Start()来做到这一点:

You wait for the task to be finished, but you never start the task. Use t.Start() to do that:
// code to define task here

t.Start();
t.Wait();


这篇关于在C#中使用任务时不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆