最近做了一套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. linux查询公网ip

    查询公网ip[出口IP] 1.curl icanhazip.com 2.curl ipecho.net/plain 3.curl www.trackip.net/i

  2. MongoDB入门(一)

    文档 文档是MongoDB中的基本数据结构,型如:{"name":"Jack","lastname":"xi"} 键值对 ...

  3. window 安装mysql

    常见错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES) 密码输入错误:无法远程 ...

  4. 深入理解JVM(三)垃圾收集器和内存分配策略

    3.1 关于垃圾收集和内存分配 垃圾收集和内存分配主要针对的区域是Java虚拟机中的堆和方法区: 3.2 如何判断对象是否“存活”(存活判定算法) 垃圾收集器在回收对象前判断其是否“存活”的两个算法: ...

  5. python中for循环的三种遍历方式

    #!/usr/bin/env python# -*- coding: utf-8 -*-if __name__ == '__main__': list = ['A', 'B', 'C', 'D'] # ...

  6. Chapter3_操作符_逻辑操作符

    逻辑操作符与(&&)或(||)非(^)能够对布尔类型的数据类型进行操作,并且生成布尔值,和关系操作符的产生的数据类型是一样的.需要注意的不多,有以下几点: (1)在需要使用string ...

  7. linux 启动tomcat卡很久的问题

    解决办法:打开$JAVA_PATH/jre/lib/security/java.security这个文件,找到下面的内容: securerandom.source=file:/dev/random 替 ...

  8. Codeforces 1077C Good Array 坑 C

    Codeforces 1077C Good Array https://vjudge.net/problem/CodeForces-1077C 题目: Let's call an array good ...

  9. VMware虚拟机网络设置

    背景介绍 在用 VMware workstation 安装好虚拟机后,需要给虚拟机配置网络,配置网络的方法有桥接.NAT.    采用桥接的方法需要占据物理机网段的ip地址,可能会与物理机同一网段的其 ...

  10. 深入理解定位父级offsetParent及偏移大小offsetTop / offsetLeft / offsetHeight / offsetWidth

    深入理解定位父级offsetParent及偏移大小 [转载] 前面的话 偏移量(offset dimension)是javascript中的一个重要的概念.涉及到偏移量的主要是offsetLeft.o ...