1. 程式人生 > 其它 >多工順序執行解決方案

多工順序執行解決方案

目錄

多工順序執行解決方案

解決思路分為四部分:

  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<Command> Commands { get; private set; }

    public CommandCollection(List<Command> 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<CommandBase> CommandQueue = new Queue<CommandBase>();
    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<CommandCollection> cmdr = new List<CommandCollection>()
    {
       new CommandCollection(new List<Command>()
       {
           new Command(){Data = "跑步的第一個任務",Delay = 5000},
           new Command(){Data = "跑步的第二個任務",Delay = 200},
           new Command(){Data = "跑步的第三個任務",Delay = 1000}
       }),
       new CommandCollection(new List<Command>()
       {
           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();
}
登峰造極的成就源於自律