任务运行时间最少。多少时间

时间:2019-12-06 13:16:37

标签: task delay

我希望有一些静态类可以调用以执行一些action1,然后等待一会儿再执行action2。就那么简单。没有取消,没有异步的把戏。像这样:

    static void Main(string[] args)
    {
        DelayedTask.Run2(() => Console.WriteLine("Hey "), new TimeSpan(0, 0, 3), () => Console.WriteLine("you!"));

        Console.WriteLine($"Press any key to end program");
        Console.ReadLine();
    }

一段时间后,我提出了4种解决方案,如下所示:

public static class DelayedTask
{
    /// <summary>
    /// Run with delay.
    /// </summary>
    /// <param name="firstAction"></param>
    /// <param name="delay"></param>
    /// <param name="lastAction"></param>
    /// <returns></returns>
    public static async Task Run1(
        Action firstAction,
        TimeSpan delay,
        Action lastAction) =>
        await Task.Factory.StartNew(() =>
        {
            firstAction();
            Thread.Sleep(delay);
        }).ContinueWith(_ => lastAction()).ConfigureAwait(false);
    public static async Task Run2(
        Action firstAction,
        TimeSpan delay,
        Action lastAction) =>
        await Task.Factory.ContinueWhenAll(new[] 
        {
            Task.Factory.StartNew(() =>
            {
                firstAction();
                Thread.Sleep(delay);
            })
        }, _ => lastAction()).ConfigureAwait(false);
    public static async Task Run3(
        Action firstAction,
        TimeSpan delay,
        Action lastAction) =>
        await Task.Factory.ContinueWhenAll(new[]
        {
            Task.Run(() =>
            {
                firstAction();
                Thread.Sleep(delay);
            })
        }, _ => lastAction()).ConfigureAwait(false);
    public static async Task Run4(
        Action firstAction,
        TimeSpan delay,
        Action lastAction) =>
        await Task.Factory.ContinueWhenAny(new[]
        {
            Task.Run(() =>
            {
                firstAction();
                Thread.Sleep(delay);
            })
        }, _ => lastAction()).ConfigureAwait(false);
}

运行Run1(...)-> 4后,它们都执行了相同的操作,在我看来,这实际上只是4种解决方案中的一种解决方案。我想念什么还是可以选择?关键特征:我想睡眠是延迟它的方法(?)-Task.Delay(...)在这里不起作用。而Run3 + 4(...)中的另一件事则无需任何t.Start(...)就可以工作。 (?)。实际上,如果我说t.Start(...)的最后一步没有执行。

Nb。假设action1运行的时间少于延迟。

2 个答案:

答案 0 :(得分:0)

您不应使用StartNewContinueWithContinueWhenAllContinueWhenAny。这些都是具有dangerous default behavior的极其底层的方法,仅供高级使用。

如果您的方法如方法签名所建议的那样是同步的,则可以执行以下操作:

public static Task Run(Action firstAction, TimeSpan delay, Action lastAction) =>
    Task.Run(() =>
    {
      firstAction();
      Thread.Sleep(delay);
      lastAction();
    });

如果需要支持异步方法,可以执行以下操作:

public static Task Run(Func<Task> firstAction, TimeSpan delay, Func<Task> lastAction) =>
    Task.Run(async () =>
    {
      await firstAction();
      await Task.Delay(delay);
      await lastAction();
    });

答案 1 :(得分:0)

感谢史蒂文:

    public static Task Run5(
        Func<Task> firstAction, 
        TimeSpan delay, 
        Func<Task> lastAction) =>
        Task.Run(async () =>
        {
            await firstAction();
            await Task.Delay(delay);
            await lastAction();
        });
    public static Task Run6(
        Action firstAction,
        TimeSpan delay,
        Action lastAction)
    {
        Func<Task> firstTask = () => { firstAction(); return Task.CompletedTask; };
        Func<Task> lastTask = () => { lastAction(); return Task.CompletedTask; };

        return Task.Run(async () =>
        {
            await firstTask();
            await Task.Delay(delay);
            await lastTask();
        });
    }