首页 > 编程语言 >C#生产流程控制(串行,并行混合执行)

C#生产流程控制(串行,并行混合执行)

时间:2023-08-19 17:24:32浏览次数:31  
标签:index 生产流程 AddMessage generator C# Text module 串行 children

开源框架CsGo

https://gitee.com/hamasm/CsGo?_from=gitee_search

 

文档资料:

https://blog.csdn.net/aa2528877987/article/details/132139337

 

实现效果

 

using Go;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;
using TaskControl.gadget;
using Module = TaskControl.gadget.Module;

namespace TaskControl
{
    public partial class MainForm : Form
    {
        //work_service work = new work_service();
        //shared_strand strand;

        control_strand _mainStrand;
        generator _timeAction;

        Dictionary<int, Module> DicModules = new Dictionary<int, Module>();
        int maxWorkerIndex = 2;
        bool continuous = false;
        bool stop = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //strand = new work_strand(work);
            //generator.go(strand, WorkerFlow);

            _mainStrand = new control_strand(this);
            _timeAction = generator.make(_mainStrand, WorkerFlow);
            //generator.tgo(_mainStrand, WorkerFlow);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearMessage();
        }

        private void btnControl_Click(object sender, EventArgs e)
        {
            AddMessage(btnControl.Text);

            switch (btnControl.Text)
            {
                case "启动任务": 
                    btnControl.Text = "暂停任务";
                    AddMessage("===== 开始生产 =====");
                    //
                    // 可配置执行顺序
                    DicModules.Clear();
                    DicModules.Add(1, module1);
                    DicModules.Add(2, module2);
                    stop = false;
                    //work.run();
                    _timeAction.run();
                    break;
                case "暂停任务":
                    btnControl.Text = "恢复任务";
                    //work.stop();
                    _timeAction.suspend();
                    break;
                case "恢复任务":
                    btnControl.Text = "暂停任务";
                    //work.reset();
                    _timeAction.resume();
                    break;
            }
        }

        private void btnContinuous_Click(object sender, EventArgs e)
        {
            AddMessage(btnContinuous.Text);

            switch (btnContinuous.Text)
            {
                case "循环生产":
                    btnContinuous.Text = "取消循环";
                    continuous = true;
                    break;
                case "取消循环":
                    btnContinuous.Text = "循环生产";
                    continuous = false;
                    break; 
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        { 
            AddMessage(btnStop.Text);

            switch (btnStop.Text)
            {
                case "结束生产":
                    btnStop.Text = "继续生产";
                    stop = true;
                    break;
                case "继续生产":
                    btnStop.Text = "结束生产";
                    stop = false;
                    break;
            }
        }

        async Task WorkerFlow()
        {
            // 罐仓同时加料
            generator.children children = new generator.children();
            foreach (var item in DicModules) 
            { 
                children.go(() => AddPot(item.Key, item.Value)); 
            } 
            await children.wait_all();
            //
            await Worker();
        }

        async Task Worker(int index = 1)
        { 
            if (index > maxWorkerIndex)
            {
                if (continuous)
                {
                    index = 1;// 循环生产

                    ClearMessage();
                }
                else
                {
                    AddMessage("===== 生产结束 =====");
                    return;
                }
            }

            AddMessage($"顺序生产:{index}");

            var module = DicModules[index];
            if (null == module) return;

            // 加料(串行)
            await AddPot(index, module);

            // 卸料(串行)
            await RemovePot(index, module); 

            generator.children children = new generator.children();
            children.go(() => MoveBelt(index)); // 皮带传输(并行) 
            children.go(() => AddPot(index, module));// 罐仓加料(并行) 
            if (!stop) children.go(() => Worker(++index)); // 继续生产 
            await children.wait_all(); 
        }

        /// <summary>
        /// 罐仓加料
        /// </summary> 
        async Task AddPot(int index, Module module)
        { 
            var currentPotVal = module.Pot.Value;
            if (currentPotVal >= module.Pot.Maximum)
            {
                AddMessage($"罐仓已满,跳过加料:【{index}】");
                return;
            }

            AddMessage($"开始加料:【{index}】");

            for (int i = currentPotVal; i <= module.Pot.Maximum; i++)
            {
                module.Pot.Value = i;
                await generator.sleep(50);
            }

            AddMessage($"结束加料:【{index}】");
        }

        /// <summary>
        /// 罐仓卸料
        /// </summary>
        async Task RemovePot(int index, Module module)
        {
            AddMessage($"开始卸料:【{index}】");

            for (int i = module.Pot.Maximum; i > 0; i--)
            {
                module.Pot.Value = i;
                await generator.sleep(50);
            }

            AddMessage($"结束卸料:【{index}】");
        }

        /// <summary>
        /// 皮带传输(工作几秒后停止-并行)
        /// </summary>
        /// <param name="index"></param> 
        async Task MoveBelt(int index)
        {
            AddMessage($"开始传输:【{index}】");

            var module = DicModules[index];
            if (null == module) return;

            module.Belt.ConveyorDirection = ConveyorDirection.Forward; 

            await generator.sleep(5000); 
            module.Belt.ConveyorDirection = ConveyorDirection.None;

            AddMessage($"结束传输:【{index}】");
        }

        public void AddMessage(string msg)
        { 
            if (IsDisposed) return;

            this.BeginInvoke((EventHandler)(delegate
            {
                if (rtbMsg.Lines.Length > 100)
                {
                    rtbMsg.Clear();
                }
                rtbMsg.AppendText(DateTime.Now.ToString("yy-MM-dd HH:mm:ss") + " " + msg + "\r\n");
                Application.DoEvents();
            }));
        }

        public void ClearMessage()
        {
            this.BeginInvoke((EventHandler)(delegate
            {
                rtbMsg.Clear();
            }));
        }
    }
}

  

标签:index,生产流程,AddMessage,generator,C#,Text,module,串行,children
From: https://www.cnblogs.com/chen1880/p/17642722.html

相关文章

  • Docker搭建lnmp之network篇
    dockerpullnginx#拉去最新的nginx镜像一、搭建vagrant+VagrantBoxVM环境创建Vagrantfile文件vagrantinit编辑Vagrantfile文件Vagrant.configure("2")do|config|config.vm.box="centos7"#指定BOXconfig.vm.networ......
  • leetcode2235 两整数相加
    题目描述:(这第一种方法我就不多说了,肯定是有手就行)给你两个整数num1和num2,返回这两个整数的和。示例1:输入:num1=12,num2=5输出:17解释:num1是12,num2是5,它们的和是12+5=17,因此返回17。示例2:输入:num1=-10,num2=4输出:-6解释:num1+num2=-6,因此返......
  • 【21.0】结合celery改造接口
    【一】引入所有接口都可以改造,尤其是查询所有的这种接口,如果加入缓存,会极大的提高查询速度首页轮播图接口:获取轮播图数据,加缓存---》咱们只是以它为例【二】改造轮播图接口luffyCity\luffyCity\apps\home\views.pyclassBannerView(GenericViewSet,CommonListMod......
  • vue.js:5108 [Vue warn]: Cannot find element: #body_container
    1、原因:我把Vue挂载元素的JS放在了html加载完成的前面了2、解决:放到html加载完成之后就可以了 ......
  • 【论文阅读】Self-Alignment with Instruction Backtranslation自对齐与指令反翻译
     Self-AlignmentwithInstructionBacktranslation自对齐与指令反翻译摘要:在当今的人工智能时代,语言模型的训练和优化已成为研究的热点。本文介绍了一种创新且可扩展的方法,通过为人编写的文本自动标注相应的指令,构建高质量的指令跟随语言模型。此研究的方法,被命名为“指令反......
  • 24届C++后端开发八月面经
    百度提前批一面项目:日志模块,如何实现保证写入和非保证写入如何保证日志时间的实时性?不用文件大小作为文件滚动的标注,而是使用时间作为标识更加符合查看日志的需求webserver如何与MYSQL数据库进行交互?当有非常多的并发量,如何进行一个MYSQL底层存储的优化?记录用户uid最......
  • Navicat执行mysql脚本报错
    1、错误日志[Err]1055-Expression#1ofORDERBYclauseisnotinGROUPBYclause andcontainsnonaggregatedcolumn'information_schema.PROFILING.SEQ' whichisnotfunctionallydependentoncolumnsinGROUPBYclause; thisisincompatiblewith......
  • 如何在控制台查看excel内容
    最近发现打开电脑的excel很慢,而且使用到的场景很少,也因为mac自带了预览的功能。但是shigen就是闲不住,想自己搞一个excel预览软件,于是在一番技术选型之后,我决定使用python在控制台显示excel的内容。具体的需要的功能有:查看excel的某一行信息查看某个范围的信息,信息的区间为[start,en......
  • elasticSearch常见的面试题
    常见的面试问题描述使用场景es集群架构3个节点,根据不同的服务创建不同的索引,根据日期和环境,平均每天递增60*2,大约60Gb的数据。调优技巧设计阶段的调优根据业务增长的需求,采取日期模版创建索引,通过rolloverAPI实现滚动索引定义条件,生成新的索引,但都指向一个别名根据别名对索引进行......
  • AP5414 DC-DC升压恒流 升降压电源驱动IC
    产品简介AP5414是一种输入电压范围宽(0.8~5.5V),可调恒定电流和限定电流两种模式来驱动白光LED而设计的升压型DC/DC变换器。该器件能利用单节或双节干电池驱动单颗大功率白光LED,同样可以利用一节锂电池驱动两颗、三颗或多颗WLED。驱动WLED串联连接的方法可以提供相等的WL......