当前位置: 首页 > news >正文

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

开源框架CsGo

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

文档资料:

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

实现效果

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

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();

            }));

        }

    }

}

http://www.lryc.cn/news/131776.html

相关文章:

  • 【广州华锐视点】VR线上教学资源平台提供定制化虚拟现实学习内容
  • 计算机视觉的应用11-基于pytorch框架的卷积神经网络与注意力机制对街道房屋号码的识别应用
  • 正则表达式:学习使用正则表达式提取网页中的目标数据
  • 最长重复子数组(力扣)动态规划 JAVA
  • JavaWeb_LeadNews_Day6-Kafka
  • ATTCK覆盖度97.1%!360终端安全管理系统获赛可达认证
  • 透视俄乌网络战之一:数据擦除软件
  • 微服务中间件--Nacos
  • 驱动开发点亮led灯
  • 回归预测 | MATLAB实现IPSO-SVM改进粒子群优化算法优化支持向量机多输入单输出回归预测(多指标,多图)
  • 数学建模之“TOPSIS数学模型”原理和代码详解
  • threejs使用gui改变相机的参数
  • 计算机竞赛 图像识别-人脸识别与疲劳检测 - python opencv
  • PHP8的字符串操作3-PHP8知识详解
  • Unity VR:XR Interaction Toolkit 输入系统(Input System):获取手柄的输入
  • 智慧工地一体化云平台源码:监管端、工地端、危大工程、智慧大屏、物联网、塔机、吊钩、升降机
  • C# 表达式体方法 C#算阶乘
  • 互联网发展历程:保护与隔离,防火墙的安全壁垒
  • 基于IMX6ULLmini的linux裸机开发系列七:中断处理流程
  • Postman软件基本用法:浏览器复制请求信息并导入到软件从而测试、发送请求
  • react go实现用户历史登录列表页面
  • 如何做好服务性能测试
  • 速通蓝桥杯嵌入式省一教程:(五)用按键和屏幕实现嵌入式交互系统
  • 虚拟拍摄,如何用stable diffusion制作自己的形象照?
  • 开启AI创新之旅!“华为云杯”2023人工智能应用创新大赛等你来挑战
  • npm和node版本升级教程
  • C++入门篇9---list
  • STM32基于CubeIDE和HAL库 基础入门学习笔记:物联网项目开发流程和思路
  • Hive on Spark (1)
  • PostgreSQL基本操作总结