最近做了一套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. 图像处理项目——生成csv文件提高读取效率

    利用pyhton脚本生成csv文件 *开发环境为windows PyCharm*使用的是pyhton脚本*生成人脸和人脸对应的标签的csv文件 一:主要步骤 1.载入对应路径2.提取每一张图片对应的位 ...

  2. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  3. 2015-2016 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2015)

    题目链接 :  http://codeforces.com/gym/100781/attachments A-Adjoin the Network 题意大概是有多棵树(也可能是一颗),现在要把它们合并 ...

  4. FortiGate上架前准备

    1.收集信息 1.网络拓扑信息(了解网络拓扑信息有助于网络方案的规划) 2.环境信息(了解部署位置.部署模式.最大吞吐.最大用户数有助于对设备性能的评估) 3.客户需求,对FortiGate部署的功能 ...

  5. CentOS 7升级Python到3.5后yum出错

    CentOS 7升级Python到3.5后,我跟以前CentOS 6一样,在/usr/bin/python创建了一个指向Python 3的软连接,然后将/usr/bin/yum的顶部的: !/usr/ ...

  6. 网站开发,推荐使用SuperSlide 插件-Tab标签切换,图片滚动,无缝滚动,焦点图

    SuperSlide 致力于解决网站大部分特效展示问题,使网站代码规范整洁,方便维护更新.网站上常用的“焦点图/幻灯片”“Tab标签切换”“图片滚动”“无缝滚动”等只需要一个SuperSlide即可解 ...

  7. CUDA[4] sample program: matrix-vector multiplication

    Use Compressed Sparse Row Format (CSR) to represent matrix #include "cuda_runtime.h" #incl ...

  8. 如何通过Git GUI将自己本地的项目上传至Github(转)

    githud是一个程序员以后成长都会使用到的,先不说很多优秀的开源框架都在这上面发布,光是用来管理自己的demo都已经让人感到很方便,用得也很顺畅.而真正让我下定决心使用github的原因是因为两次误 ...

  9. 第一节20181109 《Linux就该这么学》

    在网上发现了刘老师的linux课程,关注了一段时间感觉很好就在10月1日活动日报了第19期的班,希望自己能坚持下来学好linux.

  10. 关于SGA中的granule size

    1.什么是granule? granule直译为颗粒,ORACLE为SGA 中的组件(eg:log buffer/db buffer/large pool等)分配的最小单元为一个granule. 所以 ...