Create a Task with an Action multiple parameters

前端 未结 2 2055
时光取名叫无心
时光取名叫无心 2021-01-06 14:42

I want to add multiple parameter in a Task containing Action. I reviewed the existing stack-overflow question Create a Task with an Action

Kindly assist me

2条回答
  •  有刺的猬
    2021-01-06 15:13

    Just pass the parameters like this.

    Action action = async (msg, count) => await LoadAsync(msg, count);
    Task task = new Task(() => action("", 0)); // pass parameters you want
    

    If you want to also get return value

    Func> func = LoadAsync;
    Task task = func("", 0); // pass parameters you want
    
    var result = await task; // later in async method
    

提交回复
热议问题