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+" 已经运行!");
        }
    }
}
相关推荐
繁华似锦respect3 分钟前
C++ 设计模式之观察者模式详细介绍
linux·开发语言·c++·windows·观察者模式·设计模式·visual studio
froginwe1113 分钟前
CSS Text(文本)详解
开发语言
繁华似锦respect15 分钟前
C++ 自定义 String 类
服务器·开发语言·c++·哈希算法·visual studio
n***44317 分钟前
Java进阶:IO大全
java·开发语言·python
jtymyxmz19 分钟前
《JavaEE企业级应用开发教程(Spring+Spring MVC+Mybatis)》3.2 动态代理
java·开发语言
喵霓23 分钟前
mac系统的环境变量-bash_profile
开发语言·macos·bash
向葭奔赴♡27 分钟前
Android SharedPreferences实战指南
android·java·开发语言
CoderYanger34 分钟前
A.每日一题——2435. 矩阵中和能被 K 整除的路径
开发语言·线性代数·算法·leetcode·矩阵·深度优先·1024程序员节
我命由我1234534 分钟前
微信小程序 - 页面跳转并传递参数(使用路由参数、使用全局变量、使用本地存储、使用路由参数结合本地存储)
开发语言·前端·javascript·微信小程序·小程序·前端框架·js
️公子36 分钟前
无人直播系统-黑客主题
人工智能·c#·visual studio