到底如何在另一个线程类中修改界面中的控件内容 是否是用带参数的委托?我不明白,请高手给点简洁代码 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 完整源代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Threading;namespace Updating_UI_Controls{ public partial class Form1 : Form { private bool processingDone; private Thread myWorkerThread; public Form1() { InitializeComponent(); } private void button2_Click(object sender, EventArgs e) { processingDone = true; myWorkerThread.Join(); button2.Enabled = false; button1.Enabled = true; } private void ExecuteProcessing() { UpdateLabelDelegate labelUpdater = new UpdateLabelDelegate(UpdateLabel); this.Invoke(labelUpdater, new object[]{"Processing: Active", Color.Red}); try { while (!processingDone) { Thread.Sleep(0); this.BeginInvoke(labelUpdater, new object[] { "Processing: Still Active - " + DateTime.Now, Color.Green }); } } finally { Thread.Sleep(1000); this.BeginInvoke(labelUpdater, new object[] { "Processing: Done", Color.Black }); } } delegate void UpdateLabelDelegate(string labelText, Color textColor); private void UpdateLabel(string labelText, Color textColor) { label1.Text = labelText; label1.ForeColor = textColor; } private void button1_Click(object sender, EventArgs e) { button1.Enabled = false; button2.Enabled = true; processingDone = false; myWorkerThread = new Thread(new ThreadStart(ExecuteProcessing)); myWorkerThread.Start(); } }} 一个最简单的办法 在formload事件里面加入Control.CheckForIllegalCrossThreadCalls = false;就可以了。一般这个问题都出现在.net2005里面,我也是菜鸟只知道这么个方法~。。看看还有没有大大可以给出更好的 给你点简单的代码:定义一个函数,用来更新控件,可以带参数的void Set() { TextBox1.Text="这是线程更新的控件值"; }然后定义相应的委托delegate void A();定义线程调用的函数void MyThread() {A B= new A(Set);//指向更新函数this.Invoke(B, null);//调用函数}运行线程 Thread MyThread1; MyThread1 = new Thread(new ThreadStart(MyThread)); MyThread1.Start(); private delegate void MyMethod();void OpenDialogOnOtherThread(){MyMethod OpenDialog = new MyMethod(this.MyOpenDialog);this.Invoke(OpenDialog);}void MyOpenDialog(){OpenFileDialog openFileDialog = new OpenFileDialog();openFileDialog.ShowDialog();}private void button1_Click(object sender, EventArgs e){Thread openDialog=new Thread(new ThreadStart(this.OpenDialogOnOtherThread));openDialog.Start();} 如果要在一个线程里面设置窗口中label的文字,调用函数SetText。delegate void SetTextCallBack(string text);void SetText(string text){ if (this.InvokeRequired) { SetEnabledCallBack cb = new SetEnabledCallBack(SetText); this.Invoke(cb, new object[] { text }); } else { this.label.Text = text; }} sorry,上面的代码中的SetEnabledCallBack应该改成SetTextCallBack。从自己的代码里粘的:)delegate void SetTextCallBack(string text);void SetText(string text){ if (this.InvokeRequired) { SetTextCallBack cb = new SetTextCallBack(SetText); this.Invoke(cb, new object[] { text }); } else { this.label.Text = text; }} 谢谢各位回复的高手,不过现在有个问题,我运行的线程方法是另外一个专门类中的,在这个类里面this.Invoke不让用啊!有什么办法解决? 在你的主程序里面(即主线程所在的类里面),写个委托的函数把你的专门的类包起来。UpdateLabelDelegate labelUpdater = new UpdateLabelDelegate(UpdateLabel);this.BeginInvoke(labelUpdater, new object[]{"Processing: Active", Color.Red});...private void UpdateLabel(string labelText, Color textColor){ Class aaa = new aaa();//事例化你的专门类 aaa.UpdateUI();//调用你的更新ui的方法。 //label1.Text = labelText; //label1.ForeColor = textColor; } 我看不明白楼上的,是不是我说的不清楚?我现在面对的问题是这样的,在我的主程序里面,我要运行一个线程,这个线程的入口方发为另外一个类,假设为CLS,现在我要在这里CLS类里面修改主界面的控件内容,控件内容可能为A也可能为B,所以必须用参数传递,我想在CLS类里面委托调用主界面的方法,可是用this.Invoke出错,非静态的字段、方法或属性“System.Windows.Forms.Control.Invoke(System.Delegate, object[])”要求对象引用我不知道应该怎么解决?我的思路是否正确? delegate void SetTextCallBack(string text);public void SetText(string text){ if (this.InvokeRequired) { SetTextCallBack cb = new SetTextCallBack(SetText); this.Invoke(cb, new object[] { text }); } else { this.label.Text = text; }}把上面的代码写在主线程里。在辅助线程里直接调用SetText不行吗? C# 两个bitmap类型变量的赋值出错 请问,控件的背景图怎么做好些? panel模板的小问题 .net中调试程序如何实现多机联调?急 求救 望高手指教 菜鸟请求赐教!~~C#数据库连接 sql语句 在收到的邮件中如何截取信息 在程序中实现connection adapter的赋值,编译可以 运行错误提示是。。。(源代码) 怎么动态绑定报表文件(rdlc)里的表 关于DLL调用另1个DLL的问题 有时间的高手请帮俺看看这段代码 C#怎么样排序?应该有现成的函数吧
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;using System.Threading;namespace Updating_UI_Controls
{
public partial class Form1 : Form
{
private bool processingDone;
private Thread myWorkerThread; public Form1()
{
InitializeComponent();
} private void button2_Click(object sender, EventArgs e)
{
processingDone = true;
myWorkerThread.Join();
button2.Enabled = false;
button1.Enabled = true;
} private void ExecuteProcessing()
{
UpdateLabelDelegate labelUpdater = new UpdateLabelDelegate(UpdateLabel);
this.Invoke(labelUpdater, new object[]{"Processing: Active", Color.Red}); try
{
while (!processingDone)
{
Thread.Sleep(0);
this.BeginInvoke(labelUpdater, new object[] { "Processing: Still Active - " + DateTime.Now, Color.Green });
}
}
finally
{
Thread.Sleep(1000);
this.BeginInvoke(labelUpdater, new object[] { "Processing: Done", Color.Black });
}
} delegate void UpdateLabelDelegate(string labelText, Color textColor);
private void UpdateLabel(string labelText, Color textColor)
{
label1.Text = labelText;
label1.ForeColor = textColor;
} private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
button2.Enabled = true;
processingDone = false; myWorkerThread = new Thread(new ThreadStart(ExecuteProcessing));
myWorkerThread.Start();
}
}
}
在formload事件里面加入Control.CheckForIllegalCrossThreadCalls = false;就可以了。一般这个问题都出现在.net2005里面,我也是菜鸟只知道这么个方法~。。看看还有没有大大可以给出更好的
定义一个函数,用来更新控件,可以带参数的
void Set()
{
TextBox1.Text="这是线程更新的控件值";
}
然后定义相应的委托
delegate void A();
定义线程调用的函数
void MyThread()
{
A B= new A(Set);//指向更新函数
this.Invoke(B, null);//调用函数
}
运行线程
Thread MyThread1;
MyThread1 = new Thread(new ThreadStart(MyThread));
MyThread1.Start();
private delegate void MyMethod();void OpenDialogOnOtherThread()
{MyMethod OpenDialog = new MyMethod(this.MyOpenDialog);
this.Invoke(OpenDialog);
}void MyOpenDialog()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowDialog();
}private void button1_Click(object sender, EventArgs e)
{
Thread openDialog=new Thread(new ThreadStart(this.OpenDialogOnOtherThread));
openDialog.Start();
}
{
if (this.InvokeRequired)
{
SetEnabledCallBack cb = new SetEnabledCallBack(SetText);
this.Invoke(cb, new object[] { text });
}
else
{
this.label.Text = text;
}
}
{
if (this.InvokeRequired)
{
SetTextCallBack cb = new SetTextCallBack(SetText);
this.Invoke(cb, new object[] { text });
}
else
{
this.label.Text = text;
}
}
this.BeginInvoke(labelUpdater, new object[]{"Processing: Active", Color.Red});...private void UpdateLabel(string labelText, Color textColor)
{
Class aaa = new aaa();//事例化你的专门类
aaa.UpdateUI();//调用你的更新ui的方法。
//label1.Text = labelText;
//label1.ForeColor = textColor;
}
我不知道应该怎么解决?我的思路是否正确?
{
if (this.InvokeRequired)
{
SetTextCallBack cb = new SetTextCallBack(SetText);
this.Invoke(cb, new object[] { text });
}
else
{
this.label.Text = text;
}
}把上面的代码写在主线程里。在辅助线程里直接调用SetText不行吗?