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();
}
}
}
原文:https://www.cnblogs.com/COLD486/p/14793597.html