我剛看到3個關(guān)于TPL使用的例程,它們執(zhí)行相同的工作; 這是代碼:public static void Main(){ Thread.CurrentThread.Name = "Main"; // Create a task and supply a user delegate by using a lambda expression. Task taskA = new Task( () => Console.WriteLine("Hello from taskA.")); // Start the task. taskA.Start(); // Output a message from the calling thread. Console.WriteLine("Hello from thread '{0}'.", Thread.CurrentThread.Name); taskA.Wait();}public static void Main(){ Thread.CurrentThread.Name = "Main"; // Define and run the task. Task taskA = Task.Run( () => Console.WriteLine("Hello from taskA.")); // Output a message from the calling thread. Console.WriteLine("Hello from thread '{0}'.", Thread.CurrentThread.Name); taskA.Wait();}public static void Main(){ Thread.CurrentThread.Name = "Main"; // Better: Create and start the task in one operation. Task taskA = Task.Factory.StartNew(() => Console.WriteLine("Hello from taskA.")); // Output a message from the calling thread. Console.WriteLine("Hello from thread '{0}'.", Thread.CurrentThread.Name); taskA.Wait(); }我只是不明白為什么MS給出了三種不同的方式在運行第三方物流工作,因為他們所有的工作一樣的:Task.Start(),Task.Run()和Task.Factory.StartNew()。告訴我,是Task.Start(),Task.Run()并且Task.Factory.StartNew()所有用于同一目的或者它們有不同的意義嗎?應(yīng)該Task.Start()何時使用,何時應(yīng)該使用Task.Run()何時使用Task.Factory.StartNew()?請幫助我通過示例非常詳細(xì)地了解它們的實際用法,謝謝。
2 回答

Qyouu
TA貢獻(xiàn)1786條經(jīng)驗 獲得超11個贊
你說“不要創(chuàng)建一個任務(wù)并調(diào)用Start()”將我重定向到任何可能由Task.Start()引起的好的寫入。我需要細(xì)節(jié),因為你說要避免它。謝謝你的答復(fù)。
- 2 回答
- 0 關(guān)注
- 4923 瀏覽
添加回答
舉報
0/150
提交
取消