Sequential await VS Continuation await

前端 未结 1 1808
既然无缘
既然无缘 2021-02-19 04:48

I was wondering what is the best/correct way of writing asynchronous code that is composed of two (or more) async and dependent (the first have to finish to execute second) oper

1条回答
  •  时光说笑
    2021-02-19 05:09

    You'd want to use await if you possibly can.

    ContinueWith has a number of issues. I describe this briefly in my blog post on why ContinueWith is dangerous, which builds on my earlier blog post on why StartNew is dangerous (they share many of the same issues).

    In particular:

    • ContinueWith does not have a good default TaskScheduler. The default task scheduler is TaskScheduler.Current (not TaskScheduler.Default), and resuming on the current SynchronizationContext must be done by hand if desired.
    • ContinueWith has non-ideal default options. For asynchronous code, you would want DenyChildAttach and ExecuteSynchronously, at least.
    • ContinueWith does not understand asynchronous continuations, which generally requires an additional Unwrap call to work around this limitation.
    • ContinueWith treats its CancellationToken argument in a surprising way. More on that in this blog post of mine.

    These arguments are all summarized on my blog post on Task continuations. await does not have any of these drawbacks.

    0 讨论(0)
提交回复
热议问题