Create a Task with an Actionlt;T, T, ... ngt; multiple parameters(使用动作创建任务lt;T, T, ... ngt;多个参数)
问题描述
我想在包含 Action 的任务中添加多个参数.我查看了现有的堆栈溢出问题 Create a Task with an Action<T>
I want to add multiple parameter in a Task containing Action. I reviewed the existing stack-overflow question Create a Task with an Action<T>
请帮助我如何在 Task 的 Action 方法中传递多个参数
Kindly assist me how to pass multiple arguments in a Action method in a Task
Action<string, int> action = (string msg, int count) =>
{
Task.Factory.StartNew(async () =>
{ await LoadAsync(msg, count); });
};
Task task = new Task(action, ....);
动作方法是
public static async Task<string> LoadAsync(string message, int count)
{
await Task.Run(() => { Thread.Sleep(1500); });
Console.WriteLine("{0} {1} Exceuted Successfully !", message ?? string.Empty, (count == 0) ? string.Empty : count.ToString());
return "Finished";
}
请帮助我如何创建异步方法的操作以及如何将操作添加到任务中.
Kindly assist me how to Create a action of an async method and how to add the action into the Task.
推荐答案
像这样传参数就行了.
Action<string, int> action = async (msg, count) => await LoadAsync(msg, count);
Task task = new Task(() => action("", 0)); // pass parameters you want
如果你也想得到返回值
Func<string, int, Task<string>> func = LoadAsync;
Task<string> task = func("", 0); // pass parameters you want
var result = await task; // later in async method
这篇关于使用动作创建任务<T, T, ... n>多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用动作创建任务<T, T, ... n>多个参数
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01