[英]Manage sync calls in async method .net 4.5
I have a question regarding sync call inside an async method. 我有一个关于异步方法内的同步调用的问题。
I have 5 I/O calls in one async method. 我在一种异步方法中有5个I / O调用。 3 of them have async api that I can use but 2 of them are sync (request for web service without async api).
其中3个具有我可以使用的异步api,但其中2个具有同步功能(不带异步api的Web服务请求)。 My question is what is the best practice for this situation?
我的问题是在这种情况下的最佳做法是什么?
Task.Run
or Task.Factory.StartNew
and take thread from the pool but in the video you mentioned that it's could actually can hurt the concurrency. Task.Run
或Task.Factory.StartNew
并从池中获取线程,但是在您提到的视频中,它实际上可能会损害并发性。 I'm little bit confused what the correct way to go here. 我有点困惑什么是正确的方法。
You can utilize StartNew with a custom SynchonizationContext, or you can set the Task as LongRunning. 您可以将StartNew与自定义的SynchonizationContext结合使用,也可以将Task设置为LongRunning。 LongRunning tasks use they own thread.
LongRunning任务使用它们自己的线程。 It will hurt someway the performance (more threads running on the system overall), but will not have impact on other things running on ThreadPool.
它将以某种方式损害性能(总体上在系统上运行更多的线程),但不会对ThreadPool上运行的其他事物产生影响。
Task.Factory.StartNew(() => DoThingy(), TaskCreationOptions.LongRunning)
You can see here about TaskSchedulers http://msdn.microsoft.com/en-us/library/dd997402.aspx . 您可以在此处查看有关TaskSchedulers的信息: http://msdn.microsoft.com/zh-cn/library/dd997402.aspx 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.