namespace invoke和begininvoke的用法
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//声明接受int的方法并返回空的委托。
public delegate void invokeDelegate();
//调用委托,
// invokeDelegate FF = new invokeDelegate(StartMethod); private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "主线程1 最先执行"); // 最先执行
Thread invokeThread = new Thread(new ThreadStart(StartMethod)); //委托创建线程
invokeThread.Start(); //开始线程 string a = string.Empty;
for (int i = ; i < ; i++) //调整循环次数,看的会更清楚
{
Thread.Sleep();
a = a + "循环执行";
}
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() +"主线程1"+ a); } private void StartMethod()
{
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "主线程1 第二执行");
button1.Invoke(new invokeDelegate(invokeMethod)); //传递一个委托的方法; 依赖此控件来执行委托
//Thread.Sleep(3000);
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "子线程1");
}
private void invokeMethod()
{
//Thread.Sleep(5000);
MessageBox.Show(Thread.CurrentThread.GetHashCode().ToString() + "主线程1 _执行委托方法");
}
}
}

来源 https://www.cnblogs.com/lsgsanxiao/p/5523282.html;

线程初始化方法

--委托

        public delegate int MyDelegate(string s);  //声明委托 // 类似于函数指针  返回值 为int 的函数,
//定义一个与这个委托相对应的函数
public static int AddNum(string str)
{
return ;
}
//自定义一个 函数 来
public void exc_delegate() { // 创建委托实例
MyDelegate nc1 = new MyDelegate(AddNum);
nc1(string.Empty); //执行该函数
}

---调用控件线程

        public delegate void UpdateListBoxCallback(string strAlarmTime, string strDevIP, string strAlarmMsg);
          if (InvokeRequired)
{
object[] paras = new object[];
paras[] = DateTime.Now.ToString(); //当前PC系统时间
paras[] = strIP;
paras[] = sb.ToString();
listViewAlarmInfo.BeginInvoke(new UpdateListBoxCallback(UpdateClientList), paras);
}
      public void UpdateClientList(string strAlarmTime, string strDevIP, string strAlarmMsg)
{
//列表新增报警信息
listViewAlarmInfo.Items.Add(new ListViewItem(new string[] { strAlarmTime, strDevIP, strAlarmMsg }));
}

C# invoke和begininvoke的用法的更多相关文章

  1. 浅谈Invoke 和 BegionInvoke的用法

    很多人对Invoke和BeginInvoke理解不深刻,不知道该怎么应用,在这篇博文里将详细阐述Invoke和BeginInvoke的用法: 首先说下Invoke和BeginInvoke有两种用法: ...

  2. 转载:C# this.Invoke()的作用与用法 理解三

    Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke();   在多线程编程中,我们经常要在工作线程 ...

  3. .Net基础——程序集与CIL HttpClient封装方法 .Net Core 编码规范 C#中invoke和beginInvoke的使用 WebServeice 动态代理类

    .Net基础——程序集与CIL   1. 程序集和CIL: 程序集是由.NET语言的编译器接受源代码文件产生的输出文件,通常分为 exe和dll两类,其中exe包含Main入口方法可以双击执行,dll ...

  4. C# this.Invoke()的作用与用法

    Invoke()的作用是:在应用程序的主线程上执行指定的委托.一般应用:在辅助线程中修改UI线程( 主线程 )中对象的属性时,调用this.Invoke();   在多线程编程中,我们经常要在工作线程 ...

  5. C# Invoke或者BeginInvoke的使用

    在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate. 一.为什么Control类提供了Invoke和BeginInvoke机制? 关于这个问题的最主要的原因已经是do ...

  6. C#中Invoke 和 BeginInvoke 的区别

    Control.Invoke 方法 (Delegate) :在拥有此控件的基础窗口句柄的线程上执行指定的委托. Control.BeginInvoke 方法 (Delegate) :在创建控件的基础句 ...

  7. 控件的invoke和beginInvoke方法

    System.Windows.Forms.Timer 的timer是在主线程上执行的,因此在timer的tick事件中操作界面上的控件不会发生线程的安全性检测. Control的invoke和begi ...

  8. C#的Invoke和BeginInvoke

    在Invoke或者BeginInvoke的使用中无一例外地使用了委托Delegate,至于委托的本质请参考我的另一随笔:对.net事件的看法. 一.为什么Control类提供了Invoke和Begin ...

  9. 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke

    今天关闭一个窗体,报出这样的一个错误"在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke.",这个不用多想,肯定是那个地方没有释放掉.既然碰到这个问题, ...

随机推荐

  1. MySQL Workbench查看和修改表字段的Comment值

    查看: 选择单个表->[右键]->[Table Inspector] 再选择Columns选项卡即可,把表格拉倒最后一列. 编辑: 选择单个表->[右键]->[Alter Ta ...

  2. lua中的闭包小结

    function newCounter() return function() i=i+ return i end end c1=newCounter() print(c1()) print(c1() ...

  3. Android之设置拖拽监听

    以EditText为例: username.setOnDragListener(new OnDragListener() { @Overridepublic boolean onDrag(View v ...

  4. Redis是单线程的

    Redis是单线程的 学习了: http://blog.csdn.net/liupeng_qwert/article/details/77263187 https://www.cnblogs.com/ ...

  5. 局域网部署docker--从无到有创建自己的docker私有仓库

    因为GFW的关系.国内用户在使用docker的时候,pull一个主要的镜像都拉下来.更不用说使用官方的index镜像了.差点放弃使用docker了,google了一圈.总算找到办法. 第一步:安装do ...

  6. 【翻译自mos文章】使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式。

    使用aum( Automatic Undo Management) 时遇到 ORA-01555错误--- 原因和解决方式. 參考原文: ORA-01555 Using Automatic Undo M ...

  7. 查找两个大文件(1G以上)的相同内容PHP版

    这是是一个大文件处理,面试官出题的意图并不希望你两层for循环进行遍历,这种答案肯定是不会要的! 这道题目的解法思路是: 顺序读取两个文件的的全部记录 将每条记录经过hash->转换为10进制- ...

  8. raid5什么意思?如何做raid5?raid5 几块硬盘?

    raid5什么意思?如何做raid5?raid5 几块硬盘? 分类: 项目管理2012-09-28 00:38 5326人阅读 评论(0) 收藏 举报 一.raid什么意思?RAID是“Redunda ...

  9. JavaScript检查是否包含某个字符

    转自:http://my.oschina.net/u/1450300/blog/389325 indexOf用法: indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置.如 ...

  10. setings.py配置文件详解

    BASE_DIR指的是项目的根目录.SECRET_KEY是安全码. # SECURITY WARNING: don't run with debug turned on in production! ...