How to delay 'hot' tasks so they can processed in a set order

后端 未结 2 905
伪装坚强ぢ
伪装坚强ぢ 2021-01-15 23:28

Say I have a set of tasks:

        var task1 = DoThisAsync(...);
        var task2 = DoThatAsync(...);
        var task3 = DoOtherAsync(...);
        var tas         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-15 23:57

    Calling a method runs code. If you want an object that will call this method later, then use a delegate.

    In this case, you could use Func, which is an asynchronous delegate. A list of these should suffice:

    // Build the list of operations in order.
    var operations = new List>();
    operations.Add(() => DoThisAsync(...));
    operations.Add(() => DoThatAsync(...));
    operations.Add(() => DoOtherAsync(...));
    
    // Execute them all one at a time.
    foreach (var operation in operations)
      await operation();
    

提交回复
热议问题