• 多任务顺序执行解决方案


    多任务顺序执行解决方案

    解决思路分为四部分:

    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();
    }
    
    登峰造极的成就源于自律
  • 相关阅读:
    C++派生类中定义基类的虚函数时需注意的事项
    [wp7软件]wp7~~各种视频播放器下载大全! 集合贴~~~
    [wp7软件]wp7~~日历软件下载大全! 集合贴~~~
    [wp7软件]相机 画图 截屏 photo 集合 软件(三)
    [wp7软件]相机 画图 截屏 photo 集合 软件(二)
    [wp7软件]wp7~~天气查询软件下载大全! 集合贴~~~
    [wp7软件]相机 画图 截屏 photo 集合 软件(一)
    [wp7软件]wp7~~各种浏览器下载大全! 集合贴~~~
    [wp7软件]wp7~~各种音乐播放器下载大全! 集合贴~~~
    [wp7软件]相机 画图 截屏 photo 集合 软件(四)
  • 原文地址:https://www.cnblogs.com/fishpond816/p/15140468.html
Copyright © 2020-2023  润新知