何时使用task.run和taskfactory.startnew() [英] When to use the task.run and taskfactory.startnew()

查看:161
本文介绍了何时使用task.run和taskfactory.startnew()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,
 
I need to know two things 
 
1.I would like to know which is best to use either Task.Run or TaskFactory.StartNew(), When to use this in async Programming, What are all the Main Difference?, Can you anyone Give me the list of valid points.
 
2. Can we use all method as Task in WPF, is there any dis-advantages available while am using all method as Task in WPF?
 
 
Thanks in Advance. 





我的尝试:



搜索关于这些,但我无法得到正确的解释



What I have tried:

Searched About these all but i couldn't get proper explanation

推荐答案

1。两种方法几乎相同。如果您不需要指定TaskCreationOptions或TaskContinuationOptions或TaskScheduler,则建议使用第二个。

有关详细信息,请阅读

StartNew is Dangerous [ ^ ]



c# - Task.Run()和Task.Factory.StartNew()之间有什么区别 - Stack Overflow [ ^ ]



2.我认为在WPF中返回Task没有任何缺点。



如果您需要在方法中与UI进行交互,则必须使用Dispatcher。这是你在问什么?



如果你分享一些你的代码的例子我可以在我的答案中更具体。
1. Both methods do almost the same. Second one is recommended if you do not need to specify TaskCreationOptions or TaskContinuationOptions or TaskScheduler.
For more details you can read
StartNew is Dangerous[^]
and
c# - What is the difference between Task.Run() and Task.Factory.StartNew() - Stack Overflow[^]

2. I do not see any disadvantages in returning Task as a result in WPF.

If you need to interact with UI in your methods you have to use Dispatcher. Is it what you are asking?

If you share some examples of your code I can be more specific in my answer.


这篇关于何时使用task.run和taskfactory.startnew()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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