Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9134897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:44:48+00:00 2026-06-17T08:44:48+00:00

Using the command pattern returning tasks that are just sleeps for 5 seconds the

  • 0

Using the command pattern returning tasks that are just sleeps for 5 seconds the total for the 3 tasks to complete is ~15 seconds.

What am I doing that is keeping this code from executing in “parallel”?

The calling code

        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());

The Commands being executed

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[]>());
    }
}

The Command Abstraction

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;
    }
}

the Command Executor

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T08:44:49+00:00Added an answer on June 17, 2026 at 8:44 am

    The problem is that you aren’t waiting inside the actual Task object, you’re waiting in the method that creates the task before it actually provides that task:

    protected override void Execute()
    {
        new System.Threading.ManualResetEvent(false).WaitOne(5000);
    
        Result = Task.Factory.StartNew(() => new Dictionary<string, string[]>());
    }
    

    should instead be:

    protected override void Execute()
    {
        Result = Task.Factory.StartNew(() =>
        {
            new System.Threading.ManualResetEvent(false).WaitOne(5000);
            return new Dictionary<string, string[]>();
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF service that implements a command pattern. Using reflection, I've created
I have read that using command pattern is one of the most popular ways
I'm using the front controller pattern in conjunction with a command resolver class, whose
I tried to create jar file using command line that I inserted into java
I'm trying to call a java program from python using command line. The code
I'm developing application using Command Pattern. I have two kind of operations in my
I am using the following command to find a pattern spanning multiple lines and
My question is based around invoking a server method by using the command pattern.
I am trying to implement the command pattern to control a robot. I'm using
I am using the following command on command line for getting the pattern matched

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.