有如下一个线程类,大部分消耗时间的事情都在这个类中写了。
类名 MoreTime,其中就一个浪费时间的东西。
这是一个独立的文件MoreTime.cs
- C# code
namespace ThreadTest
{
public class MoreTime
{
public void WaitMoreTime()
{
for (int i ; i<500;i++)
{
DoSomething()
Thread.sleep(5000)
}
}
}
}
现在,我想在主界面中,把其中的WaitMoreTime 里的 i 值传回UI线程,我要在一个 LixtBox中显示他们。
下面是启动线程的代码: 在 Form1.cs 中。
- C# code
namespace ThreadTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MoreTime mt = new MoreTime();
ThreadStart start = new ThreadStart(mt.WaitMoreTime);
Thread thread = new Thread(start);
thread.Start();
}
}
}
可是,要怎样改才能让多线程 thread 的WaitMoreTime里的i值能传回到主界面中,并显示在一个 ListBox 中呢?