This.invoke和this.begininvoke的区别?

This.invoke和this.begininvoke的区别?Thread 调用 Control 的 BeginInvoke privateThrea privatedeleg privatevoidS C 代码段 Control BeginInvoke newbeginInvo beginInvokeM D 代码段

②通常可以把耗时的操作放到子线程里执行,然后再调用BeginInvoke/Invoke方法

③Control的BeginInvoke是相对于工作线程(工作线程是调用BeginInvoke方法的线程)是异步的。
工作线程不管是UI线程还是子线程,Invoke需要阻塞工作线程执行,而BeginInvoke不需要阻塞工作线程执行。

UI线程:

 private void button76_Click(object sender, EventArgs e) { 
    Console.WriteLine("1"); this.Invoke(new Action(() => { 
    Thread.Sleep(1000); Console.WriteLine("2"); })); Console.WriteLine("3"); } private void button85_Click(object sender, EventArgs e) { 
    Console.WriteLine("4"); this.BeginInvoke(new Action(() => { 
    Thread.Sleep(1000); Console.WriteLine("5"); })); Console.WriteLine("6"); } } 

子线程:

 private void button76_Click(object sender, EventArgs e) { 
    Thread t = new Thread(()=> { 
    Console.WriteLine("1"); this.Invoke(new Action(() => { 
    Thread.Sleep(10000); Console.WriteLine("2"); })); Console.WriteLine("3"); }); t.Start(); } private void button85_Click(object sender, EventArgs e) { 
    Thread t = new Thread(() => { 
    Console.WriteLine("4"); this.BeginInvoke(new Action(() => { 
    Thread.Sleep(10000); Console.WriteLine("5"); })); Console.WriteLine("6"); }); t.Start(); } } 

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/222416.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午3:50
下一篇 2026年3月17日 下午3:51


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号