应该比较简单的,用BackgroundWorker的例子Safe, Even Simpler Multithreading in Windows Forms 2.0 http://www.mikedub.net/mikeDubSamples/SafeReallySimpleMultithreadingInWindowsForms20/SafeReallySimpleMultithreadingInWindowsForms20.htm以及 ChrisSells用Invoke的三篇文章 http://msdn2.microsoft.com/en-us/library/ms993020.aspx
public partial class Form1 : Form { private delegate void UIOperater(string str); Thread thrd; public Form1() { InitializeComponent(); } private void btnOK_Click(object sender, EventArgs e) { //打开一个线程 if (thrd == null || !thrd.IsAlive) { thrd = new Thread(new ThreadStart(ChangeBtnTxt)); thrd.Start(); } } private void ChangeBtnTxt() { uint i = 0; while (true) { i++; i = i % 10000; this.Invoke(new UIOperater(SetBtnTxt), new object[] { i.ToString() }); Thread.Sleep(100); } } private void SetBtnTxt(string str) { this.btnOK.Text = str; } }
一点非分要求。请使用多线程 委托 示一例。
在使用 微软的组件 bgwork 示一例。烦请。 在后台操作的费时任务中 完成类似。 拷贝100个大文件之类的。 在主界面上能够实时显示 正在拷贝的文件名。 谢谢大家。
http://www.mikedub.net/mikeDubSamples/SafeReallySimpleMultithreadingInWindowsForms20/SafeReallySimpleMultithreadingInWindowsForms20.htm以及 ChrisSells用Invoke的三篇文章
http://msdn2.microsoft.com/en-us/library/ms993020.aspx
public partial class Form1 : Form
{
private delegate void UIOperater(string str);
Thread thrd; public Form1()
{
InitializeComponent();
} private void btnOK_Click(object sender, EventArgs e)
{
//打开一个线程
if (thrd == null || !thrd.IsAlive)
{
thrd = new Thread(new ThreadStart(ChangeBtnTxt));
thrd.Start();
}
} private void ChangeBtnTxt()
{
uint i = 0;
while (true)
{
i++;
i = i % 10000;
this.Invoke(new UIOperater(SetBtnTxt), new object[] { i.ToString() });
Thread.Sleep(100);
}
} private void SetBtnTxt(string str)
{
this.btnOK.Text = str;
}
}
//定义代理ShowStatus
private delegate void ShowStatus(string str);
ShowStatus showStatus = new ShowStatus(DoShowStatus);//用来更新界面元素的函数
private void DoShowStatus(string txt)
{
lock (this)
{
//sta_SysStatus是界面控件
sta_SysStatus.Text = txt;
}
}//调用 this.BeginInvoke(showStatus);
private delegate void ShowStatus(string str);
ShowStatus showStatus = new ShowStatus(DoShowStatus);//用来更新界面元素的函数
private void DoShowStatus(string txt)
{
lock (this)
{
//sta_SysStatus是界面控件
sta_SysStatus.Text = txt;
}
}//调用
string str = "需要显示的文字";
this.BeginInvoke(showStatus, str);