locked
collection of methods to run AWAIT sequentially RRS feed

  • Question

  • Hi,

    I have a bunch of async Task<T>methods like:

    public async Task<string> Method1(int i, string s){ return s;}
    public async Task<string> Method2() {return "out";}
    public async Task<bool> Method3() {return true;}


    Is there a way I can create a collection like:

    public class MethodsCollection
    {
        public string PreText { get; set; }
        public Delegate FunctionName { get; set; }
    }

    List<MethodsCollection> methods = new List<MethodsCollection>();
    methods.Add(new MethodsCollection{    PreText = "",    FunctionName = new Func<int, string, Task<string>>(Method1)});
    methods.Add(new MethodsCollection{    PreText = "",    FunctionName = new Func<string, Task<string>>(Method2)});
    methods.Add(new MethodsCollection{    PreText = "",    FunctionName = new Func<Task<bool>>(Method3)});

    and then loop through every method - BUT using AWAIT - I want them to run sequentially:


    Thanks in advance.
    Harry


    Tuesday, August 25, 2020 9:32 PM

Answers

  • Hi harry_777,

    Thank you for posting here.

    The three methods have different parameters and return values, how should we use them in the loop?

    I only know it can be used like this:

      var re1 = ((Task<string>)methods[0].FunctionName.DynamicInvoke(1, "test")).Result;
      var re2 = ((Task<string>)methods[1].FunctionName.DynamicInvoke()).Result;
      var re3 = ((Task<bool>)methods[2].FunctionName.DynamicInvoke()).Result;
    
    There is another small problem. Method2 has no parameters, so
    new Func<string, Task<string>>(Method2)});

    should be
    new Func<Task<string>>(Method2)});

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by CoolDadTx Wednesday, August 26, 2020 2:46 PM
    • Marked as answer by harry_777 Sunday, September 6, 2020 7:45 AM
    Wednesday, August 26, 2020 2:35 AM

All replies

  • Hi harry_777,

    Thank you for posting here.

    The three methods have different parameters and return values, how should we use them in the loop?

    I only know it can be used like this:

      var re1 = ((Task<string>)methods[0].FunctionName.DynamicInvoke(1, "test")).Result;
      var re2 = ((Task<string>)methods[1].FunctionName.DynamicInvoke()).Result;
      var re3 = ((Task<bool>)methods[2].FunctionName.DynamicInvoke()).Result;
    
    There is another small problem. Method2 has no parameters, so
    new Func<string, Task<string>>(Method2)});

    should be
    new Func<Task<string>>(Method2)});

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by CoolDadTx Wednesday, August 26, 2020 2:46 PM
    • Marked as answer by harry_777 Sunday, September 6, 2020 7:45 AM
    Wednesday, August 26, 2020 2:35 AM
  • As Timon mentioned each method has a different return value so how exactly do you intend to get the results back? If you don't care then treat them all as Task.

    var tasks = new List<Task>() {
    
    tasks.Add(Method1(...));
    tasks.Add(Method2(...));
    ...
    
    foreach (var task in tasks)
    {
       await task;
    };
    
    //No way to get results though...

    But you said you want to run them sequentially and at that point this no longer makes sense. As soon as you call the method the work has started (and potentially finished) so they wouldn't be called "sequentially" anyway. If you want them to run sequentially then you'd await each one in turn.

    //Run the first one only
    await Method1(...);
    
    //First one done, now wait again
    await Method2(...);

    This is the only way to get them to run sequentially. Unless one method relies on the results of the previous method then there would be no benefit in doing this any other way. 

    Please clarify exactly what you mean by "sequentially" and explain how awaiting each one in turn does not solve this problem for you?


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, August 26, 2020 2:51 PM