在C#中子线程如何操作主线程中窗体上控件

2024-12-30 15:23:23
推荐回答(1个)
回答1:

用委托来完成:

public partial class Form1 : Form
{
Thread T;
public Form1()
{
InitializeComponent();
T = new Thread(new ThreadStart(GuidProc));
}

private void button1_Click(object sender, EventArgs e)
{
T.Start();
}
private delegate void SetLabelText(string value);
private void SetLabel1(string value)
{
if (this.InvokeRequired)
{
SetLabelText d = new SetLabelText(SetLabel1);
object arg = (object)要传入的参数值;
this.Invoke(d,arg);
}
else
{
this.label1.Text = value;
}
}
private void GuidProc()
{
while (true)
{
SetLabel1(Guid.NewGuid().ToString());
label1.Refresh();
}
}
}