zoukankan      html  css  js  c++  java
  • 多任务顺序执行解决方案

    多任务顺序执行解决方案

    解决思路分为四部分:

    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();
    }
    
    登峰造极的成就源于自律
  • 相关阅读:
    [v]Linux下安装Git
    Ubuntu12.04 安装PyCharm
    IE11 Enterprise Mode
    Ubuntu 14.04 安装nVidia驱动后不能进入图形界面的恢复过程
    VirtualBox中安装Ubuntu12.04/Ubuntu14.04虚拟机
    Notepad++配置Python开发环境
    boost 1.56.0 编译及使用
    关于"The dependency was added by the project system and cannot be removed" Error
    [v]Windows下Git安装指南
    Windows开发环境搭建(安装 VS2010, VS2013, VS2015 Community, Windows Server 2008 R2)
  • 原文地址:https://www.cnblogs.com/fishpond816/p/15140468.html
Copyright © 2011-2022 走看看