-3

使用命令模式返回仅休眠 5 秒的任务,完成 3 个任务的总数约为 15 秒。

我在做什么阻止这段代码“并行”执行?

调用代码

        var timer = new Stopwatch();

        timer.Start();

        var task = CommandExecutor.ExecuteCommand(new Fake());
        var task2 = CommandExecutor.ExecuteCommand(new Fake());
        var task3 = CommandExecutor.ExecuteCommand(new Fake());

        Task.WaitAll(new Task[]
        {
            task, task2, task3
        });

        timer.Stop();

        Debug.Print("{0}ms for {1},{2},{3} records", timer.ElapsedMilliseconds, task.Result.Count, task2.Result.Count(), task3.Result.Count());

正在执行的命令

public class Fake : Command<Task<Dictionary<string, string[]>>>
{
    public override string ToString()
    {
        return string.Format("Fake");
    }

    protected override void Execute()
    {
        new System.Threading.ManualResetEvent(false).WaitOne(5000);

        Result = Task.Factory.StartNew(() => new Dictionary<string, string[]>());
    }
}

命令抽象

public abstract class Command
{
    public void Run()
    {
        try
        {
            var timer = new Stopwatch();
            timer.Start();
            //Debug.Print("{0}-{1}", ToString(), "Executing");
            Execute();
            timer.Stop();
            Debug.Print("{0}-{1} Duration: {2}ms", ToString(), "Done", timer.ElapsedMilliseconds.ToString(CultureInfo.InvariantCulture));             
        }
        catch (Exception ex)
        {
            Debug.Print("Error processing task:" + ToString(), ex);
        }
    }

    public abstract override string ToString();

    protected abstract void Execute();
}

/// <summary>
/// A command with a return value
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class Command<T> : Command
{
    public T Result { get; protected set; }

    public T GetResult()
    {
        Run();
        return Result;
    }
}

命令执行者

public class CommandExecutor
{
    /// <summary>
    ///     Executes the command.
    /// </summary>
    /// <param name="cmd">The CMD.</param>
    public static void ExecuteCommand(Command cmd)
    {
        cmd.Run();
    }

    /// <summary>
    ///     Executes the command for commands with a result.
    /// </summary>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    /// <param name="cmd">The CMD.</param>
    /// <returns></returns>
    public static TResult ExecuteCommand<TResult>(Command<TResult> cmd)
    {
        ExecuteCommand((Command) cmd);

        return cmd.Result;
    }
4

1 回答 1

3

问题是您不是在实际Task对象中等待,而是在创建任务的方法中等待它实际提供该任务:

protected override void Execute()
{
    new System.Threading.ManualResetEvent(false).WaitOne(5000);

    Result = Task.Factory.StartNew(() => new Dictionary<string, string[]>());
}

应该改为:

protected override void Execute()
{
    Result = Task.Factory.StartNew(() =>
    {
        new System.Threading.ManualResetEvent(false).WaitOne(5000);
        return new Dictionary<string, string[]>();
    });
}
于 2013-01-16T19:55:06.653 回答