首页 > 编程语言 > 详细

跨线程访问控件invoke和begininvoke区别实例

时间:2021-05-21 17:42:33      阅读:7      评论:0      收藏:0      [点我收藏+]

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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void CrossDelegate(string text);
private void Done(string text)
{
textBox1.Text = text;
Thread.Sleep(2000);
}
private void SetLabel()
{
CrossDelegate dl = new CrossDelegate(Done);
string text = "test";
//BeginInvoke(dl, text); // 异步调用委托,调用后立即返回并立即执行下面的语句
this.Invoke(dl, text); // 等待工作线程完成后, 才接着执行下面的语句.
MessageBox.Show("委托调用返回了", "观察委托调用的反应效果");

}
private void button1_Click(object sender, EventArgs e)
{
Thread newThread = new Thread(new ThreadStart(SetLabel));
newThread.Start();
}
}
}

跨线程访问控件invoke和begininvoke区别实例

原文:https://www.cnblogs.com/COLD486/p/14793597.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!