最近做了一套MES集成系统,由上料到成品使自动化运行,其中生产过程是逐步的,但是每一个动作都需要独立的线程进行数据监听,那么就需要实现线程等待。

代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace Forms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
} CountdownEvent latch ;
private void refreshData(CountdownEvent latch)
{
latch.Signal();
} delegate void UpdateText(Control control, string text);
UpdateText color = delegate (Control control, string text)
{
control.Text = text;
}; private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(ProcessMain);
th.IsBackground = true;
th.Start();
} /// <summary>
/// 主线程函数
/// </summary>
void ProcessMain()
{
Process("");
Process("");
Process("");
MessageBox.Show("全部执行完毕");
} /// <summary>
/// 动作
/// </summary>
void Process(string value)
{
latch = new CountdownEvent();
Thread thread = new Thread(() => {
bool flag = true;
int i = ;
while (flag)
{
if (i > )
{
flag = false;
}
i++;
Thread.Sleep();
}
refreshData(latch);
});
thread.Start();
latch.Wait();
this.Invoke(color, new object[] { textBox1, value });
}
}
}

多个线程全部执行完毕后再调用方法:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms; namespace ExecutionCourseSystem
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
} CountdownEvent latch = new CountdownEvent();
private void Form3_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(() => refreshData());
thread.Start();
thread = new Thread(() => refreshData());
thread.Start();
thread = new Thread(() => refreshData());
thread.Start();
thread = new Thread(() => refreshData());
thread.Start();
thread = new Thread(() => refreshData());
thread.Start();
latch.Wait();
MessageBox.Show("线程全部执行完毕");
}
private void refreshData()
{
latch.Signal();
}
}
}

C# 本进程执行完毕后再执行下一线程的更多相关文章

  1. Java主线程在子线程执行完毕后再执行

    一.join() Thread中的join()方法就是同步,它使得线程之间由并行执行变为串行执行. public class MyJoinTest { public static void main( ...

  2. 用一个bat文件调用另外两个bat文件,当1.bat执行完后再执行2.bat

    用一个bat文件调用另外两个bat文件,当1.bat执行完后再执行2.bat 摘自:https://zhidao.baidu.com/question/492732911.html @echo off ...

  3. JAVA设计方法思考之如何实现一个方法执行完毕后自动执行下一个方法

    今天编程时,突然想起来在一些异步操作或Android原生库的时候,需要我们实现一些方法, 这些方法只需要我们具体实现,然后他们会在适当的时候,自动被调用! 例如AsyncTask,执行玩doInBac ...

  4. ajax请求执行完成后再执行其他操作(jQuery.page.js插件使用为例)

    就我们做知,ajax强大之处在于它的异步请求,但是有时候我们需要ajax执行彻底完成之后再执行其他函数或操作 这个时候往往我们用到ajax的回调函数,但是假如你不想或者不能把接下来的操作写在回调函数中 ...

  5. iOS AFNetWorking中block执行完后再执行其它操作

    需求:同时进行两次网络请求,网络请求是异步的,在网络请求成功后进行其它的操作.两个网络请求是这样,一个网络请求中block执行完之后,再进行其它操作,也是一样的原理,只是这时候不需要线程组了,只需要信 ...

  6. vue在一个方法执行完后再执行另一个方法

    vue在一个方法执行完后执行另一个方法 用Promise来实现.Promise是ES6的新特性,用于处理异步操作逻辑,用过给Promise添加then和catch函数,处理成功和失败的情况 ES7中新 ...

  7. ES6(Promise)等一个函数执行完后再执行另一个函数

    function text1(){ return new Promise((resolve, reject) => { setTimeout(function () { resolve(cons ...

  8. mysql定时计划任务,ON COMPLETION [NOT] PRESERVE 当单次计划任务执行完毕后或当重复性的计划任务执行到了ENDS阶段。而声明PRESERVE的作用是使事件在执行完毕后不会被Drop掉

    当为on completion preserve 的时候,当event到期了,event会被disable,但是该event还是会存在当为on completion not preserve的时候,当 ...

  9. React在componentWillMount中请求接口数据结束后再执行render

    1.在getInitialState中初始化isloading,初始值false getInitialState() { return { editionid: '', isloading:false ...

随机推荐

  1. python获取文件夹的大小(即取出所有文件计算大小)

    import os path = r'/Users/authurchen/PycharmProjects/Demo' # print(os.listdir(path)) ls = os.listdir ...

  2. 语义分割之Dual Attention Network for Scene Segmentation

    Dual Attention Network for Scene Segmentation 在本文中,我们通过 基于自我约束机制捕获丰富的上下文依赖关系来解决场景分割任务.       与之前通过多尺 ...

  3. matplotlib坐标轴的一些操作

  4. jqGrid的userData的用法!!!

    在一次项目中想从后台自定义一些返回值传回jqGrid,所以就想到了jqGrid的这个userData属性,但是真的是坑了我好惨,这里记录一下! 1.首先看说明,这个jsonReader的默认配置,us ...

  5. [leetcode]83. Remove Duplicates from Sorted List有序链表去重

    Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1 ...

  6. 虚拟机下 centos7 无法连接网络

    [root@localhost ~]# cd /etc/sysconfig/network-scripts [root@localhost network-scripts]# ls ifcfg-ens ...

  7. Fabric的权限管理:Attribute-Based Access Control

    之前稍微了解过Client Identity Chaincode Library,这几天正好开始实际应用. 虽然了解过,还是发现了不少之前理解的不足,也踩了不少坑. 先列出官方介绍: https:// ...

  8. exchange 2010 数据库管理

    1. 查看数据库中空白空间 Get-MailboxDatabase databasename -Status | FL AvailableNewMailboxSpace 2.卸载数据库 Dismoun ...

  9. 201771010142 张燕《面向对象程序设计(java)》第一周学习总结

    201771010142 张燕<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.co ...

  10. 走进JDK(五)------AbstractList

    接下来的一段时间重点介绍java.util这个包中的内容,这个包厉害了,包含了collection与map,提供了集合.队列.映射等实现.一张图了解java中的集合类: AbstractList 一. ...