When to use TaskEx.Run vs. TaskEx.RunEx

后端 未结 3 1173
遥遥无期
遥遥无期 2021-02-06 03:00

I\'m trying to understand when to use TaskEx.Run. I have provided two code sample i wrote below that produce the same result. What i fail to see is why i would ta

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-06 03:54

    Use TaskEx.Run when you want to run synchronous code in a thread pool context.

    Use TaskEx.RunEx when you want to run asynchronous code in a thread pool context.

    Stephen Toub has two blog posts related to the difference in behavior:

    • Potential pitfalls to avoid when passing around async lambdas
    • Task.Run vs Task.Factory.StartNew

    This is only one of several options you have for creating tasks. If you do not have to use Run/RunEx, then you should not. Use simple async methods, and only use Run/RunEx if you need to run something in the background.

提交回复
热议问题