TPL TaskFactory.FromAsync vs Tasks with blocking methods

前端 未结 2 554
感动是毒
感动是毒 2020-12-02 16:02

I was wondering if there were any performance implications between using TPL TaskFactory.FromAsync and using TaskFactory.StartNew on blocking versi

2条回答
  •  春和景丽
    2020-12-02 16:46

    You absolutely want to use FromAsync when an API offers a BeginXXX/EndXXX version of a method. The difference is that, in the case of something like Stream or Socket or WebRequest, you'll actually end up using async I/O underneath the covers (e.g. I/O Completion Ports on Windows) which is far more efficient than blocking multiple CPU threads doing a synchronous operation. These methods provide the best way to achieve I/O scalability.

    Check out this section of the .NET SDK on MSDN entitled TPL and Traditional .NET Asynchronous Programming for more information on how to combine these two programming models to achieve async nirvana.

提交回复
热议问题