多任务顺序执行解决方案


目录
  • 多任务顺序执行解决方案
  • 具体实现

多任务顺序执行解决方案

解决思路分为四部分:

  1. 创建所有任务的基类:CommandBase

  2. 创建单个任务类:Command

  3. 创建任务集合类:CommandCollection

  4. 创建任务管理中心,对所有任务的运行状态进行管理:CommandHerper

具体实现

CommandBase

public abstract class CommandBase
{
    public abstract void Execute(ref bool isStop);
}

Command

public class Command : CommandBase
{
    public string Data { get; set; }
    public int Delay { get; set; }

    public override void Execute(ref bool isStop)
    {
        if (Delay > 0) Thread.Sleep(this.Delay);
        Console.WriteLine($"正在执行任务:{Data}   延时:{Delay}秒");
    }
}

CommandCollection

public class CommandCollection : Command
{
    public List Commands { get; private set; }

    public CommandCollection(List commands)
    {
        Commands = commands;
    }

    public override void Execute(ref bool isStop)
    {
        if (Commands.Count == 0) return;
        Console.WriteLine($"任务队列开始执行");
        foreach (var cmd in Commands)
        {
            if (isStop)
            {
                Commands.Clear();
                return;
            }
            cmd.Execute(ref isStop);
        }
        Console.WriteLine($"任务队列执行结束");
    }
}

CommandHerper

public static class CommandHelper
{
    public static Queue CommandQueue = new Queue();
    public static bool Running, Stopped, IsCycle;
    public static Thread CmdThread;
    public static Task CmdTask;

    public static void AddCommand(CommandBase cmd)
    {
        lock (CommandQueue)
        {
            CommandQueue.Enqueue(cmd);
        }
    }

    public static void Start()
    {
        if (Running) return;
        Stopped = false;
        Running = true;
        //CmdTask = new Task(ProcessCommandTask);
        //CmdTask.Start();
        CmdThread = new Thread(ProcessCommandTask);
        CmdThread.Start();
    }

    public static void Stop()
    {
        if (Running)
        {
            Stopped = true;
            Running = false;
            try
            {
                #pragma warning disable 618
                    if (CmdThread != null) CmdThread.Abort();
                #pragma warning restore 618
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

    private static void ProcessCommandTask()
    {
        while (!Stopped)
        {
            CommandBase cmd;

            lock (CommandQueue)
            {
                if (CommandQueue.Count > 0)
                {
                    cmd = CommandQueue.Dequeue();
                }
                else
                {
                    break;
                }
            }

            if (cmd == null)
            {
                Thread.Sleep(10);
            }
            else
            {
                try
                {
                    do
                    {
                        cmd.Execute(ref Stopped);
                    } while (IsCycle && !Stopped);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    lock (CommandQueue)
                    {
                        CommandQueue.Clear();
                    }
                    break;
                }
            }
        }
    }
}

Program

private static void CreateCommand()
{
    List cmdr = new List()
    {
       new CommandCollection(new List()
       {
           new Command(){Data = "跑步的第一个任务",Delay = 5000},
           new Command(){Data = "跑步的第二个任务",Delay = 200},
           new Command(){Data = "跑步的第三个任务",Delay = 1000}
       }),
       new CommandCollection(new List()
       {
           new Command(){Data = "游泳的第一个任务",Delay = 3000},
           new Command(){Data = "游泳的第二个任务",Delay = 2000},
           new Command(){Data = "游泳的第三个任务",Delay = 1000}
       })
    };

    foreach (var cmd in cmdr)
    {
        CommandHelper.AddCommand(cmd);
    }
}

private static void Main()
{
    Console.WriteLine("Hello World!");
    CreateCommand();
    //CommandHelper.IsCycle = true;
    CommandHelper.Start();
    Console.Read();
}
C