推迟任务执行

本文关键字:执行 任务 推迟 | 更新日期: 2023-09-27 18:26:13

我正在玩任务,我想推迟任务的执行。

我有一个这样的示例方法:

private async Task<bool> DoSomething(string name, int delayInSeconds)
{
    Debug.WriteLine($"Inside task named: {name}");
    await Task.Delay(TimeSpan.FromSeconds(delayInSeconds));
    Debug.WriteLine($"Finishing task named: {name}");
    return true;
}

我想先创建一些任务,然后执行一些作业,然后运行这些任务。当Task<bool> myTask = DoSomething("Name", 4);行立即启动任务时,我想到了这样的东西:

string[] taskNames = new string[2];
Task<Task<bool>>[] myTasks = new Task<Task<bool>>[2];
myTasks[0] =  new Task<Task<bool>>(async () => await DoSomething(taskNames[0], taskNames[0].Length));
myTasks[1] =  new Task<Task<bool>>(async () => await DoSomething(taskNames[1], taskNames[1].Length));
// I think I can declare it also like this, but this will create tasks later
//IEnumerable<Task<Task<bool>>> myTasks = taskNames.Select(x => new Task<Task<bool>>(async () => await DoSomething(x, x.Length)));
taskNames[0] = "First";
taskNames[1] = "Second";
Debug.WriteLine($"Tasks created");
var results = await Task.WhenAll(myTasks.Select(x => { x.Start(); return x.Unwrap(); }));
Debug.WriteLine($"Finishing: {results.Select(x => x.ToString()).Aggregate((a,b) => a + "," + b) }");

这能以不同的方式完成吗,而不需要包装任务?

推迟任务执行

您可以使用Task生成的委托来稍微简化一些事情:

string[] taskNames = new string[2];
Func<Task<bool>>[] myTasks = new Func<Task<bool>>[2];
myTasks[0] = new Func<Task<bool>>(async () => await DoSomething(taskNames[0], taskNames[0].Length));
myTasks[1] = new Func<Task<bool>>(() => DoSomething(taskNames[1], taskNames[1].Length)); // Shorter version, near-identical functionally.
// I think I can declare it also like this, but this will create tasks later
//IEnumerable<Task<Task<bool>>> myTasks = taskNames.Select(x => new Task<Task<bool>>(async () => await DoSomething(x, x.Length)));
taskNames[0] = "First";
taskNames[1] = "Second";
Debug.WriteLine($"Tasks created");
var results = await Task.WhenAll(myTasks.Select(x => x()));
Debug.WriteLine($"Finishing: {results.Select(x => x.ToString()).Aggregate((a, b) => a + "," + b) }");

注意事项:当您调用这些委托时,DoSomething将同步执行到第一个await,因此行为是相似的,但并非完全相同。

或者,基于IEnumerable的解决方案也可以正常工作。只需编写一个迭代器方法和yield return任务即可。

就我个人而言,我会这么做:

string[] taskNames = new string[2];
taskNames[0] = "First";
taskNames[1] = "Second";
var results = await Task.WhenAll(taskNames.Select(n => DoSomething(n, n.Length)));
Debug.WriteLine($"Finishing: {results.Select(x => x.ToString()).Aggregate((a, b) => a + "," + b) }");   

在您的示例中,您不仅仅是在"创建Task"。您正在调用一个方法DoSomething将返回一个Task。假设这些是async方法,那么Task的创建和启动将在编译器生成的代码中进行。

这个问题的解决方案很简单:在准备好运行该方法之前,不要调用该方法。想象一下,在任何其他情况下,你所要求的行为会有多令人困惑。