this.Invoke和this.BeginInvoke的区别

c# Invoke和BeginInvoke 区别

本文导读:BeginInvoke() 调用时,当前线程会启用线程池中的某个线程来执行此方法,当前线程不被阻塞,继续运行后面的代码, Invoke() 调用时,会阻塞当前线程,等到 Invoke() 方法返回才继续执行后面的代码,而这两个方法中执行的是一个委托。

一、c# Invoke和BeginInvoke的认识

1、Invoke() 调用时,Invoke会阻止当前主线程的运行,等到 Invoke() 方法返回才继续执行后面的代码,表现出"同步"的概念。

3、BeginInvoke() 调用时,当前线程会启用线程池中的某个线程来执行此方法,BeginInvoke不会阻止当前主线程的运行,而是等当前主线程做完事情之后再执行BeginInvoke中的代码内容,表现出"异步"的概念。

4、EndInvoke() ,在想获取 BeginInvoke() 执行完毕后的结果时,调用此方法来获取。

二、通过实例说明 c# Invoke和BeginInvoke的区别

1、实例

C# 代码 复制

cs 复制代码
 private void button1_Click(object sender, EventArgs e)     
     {
      this.textBox1.Text = "1";

         this.Invoke(new EventHandler(delegate       {
            this.textBox1.Text += "2";
         }));

         this.textBox1.Text += "3";
      }

结果为:123

2、实例

C# 代码 复制

cs 复制代码
  private void button1_Click(object sender, EventArgs e)     
     {
      this.textBox1.Text = "1";

         this.BeginInvoke(new EventHandler(delegate       {
            this.textBox1.Text += "2";
         }));

         this.textBox1.Text += "3";
      }

结果为: 132

3、实例

C# 代码 复制

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace InvokeTest
{
    delegate void MsgDelegate(String str);//声明一个代理

    public partial class Form1 : Form
    {
        Thread[] threads = new Thread[10];//如果不使用关键字new出对象来,则会报"未将对象引用设置到对象的实例。"的错误
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Thread.CurrentThread.Name = "主线程";

            for(int i=0;i<10;i++)
            {  Thread ts = new Thread(new ThreadStart(threadProc));  ts.Name = "线程组成员"+i.ToString()+"号";  threads[i] = ts;   ts.IsBackground = true;  ts.Start();  }
           
        }

        private void showMsg(String str)
        {
            Thread.Sleep(5000);//线程休眠5秒...
            MessageBox.Show(str+"是在"+Thread.CurrentThread.Name+"中执行的");//显示执行该代理函数的是哪个线程
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("执行A1代码段");

            Thread T1 = new Thread(threadProc1);
            T1.Name = "线程一";
            T1.Start();

            MessageBox.Show("执行B1代码段");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("执行A2代码段");

            Thread T2 = new Thread(threadProc2);
            T2.Name = "线程二";
            T2.Start();

            MessageBox.Show("执行B2代码段");
        }

        /// <summary>
        /// 线程一的线程函数
        /// </summary>
        private void threadProc1()
        {
            Invoke(new MsgDelegate(showMsg), new object[] { "通过Invoke调用showMsg" });//相当于this.Invoke,调用要消耗较长时间的代码后完了才开始执行下一条语句
            MessageBox.Show("Invoke函数后调用,因为Invoke是同步执行的,所以刚才我一直在等待showMsg执行完呢,唉...");
        }

        /// <summary>
        /// 线程二的线程函数
        /// </summary>
        private void threadProc2()
        {
            BeginInvoke(new MsgDelegate(showMsg), new object[] { "通过BeginInvoke调用showMsg" });//相当于this.BeginInvoke()调用要消耗较长时间的代码后立刻执行下一条语句
            MessageBox.Show("BeginInvoke函数后调用,但由于BeginInvoke的异步执行,故还没等showMsg执行完我就被执行了!哈哈...");
        }
        private void threadProc()
        {
            MessageBox.Show(Thread.CurrentThread.Name+" 已经运行!");
        }
    }
}
相关推荐
hez20103 小时前
Runtime Async - 步入高性能异步时代
c#·.net·.net core·clr
mudtools16 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫20 小时前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools1 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox