//先为窗体添中三个控件,两个button,和一个listBox,默认属性就可以了.
using System;
using System.Windows.Forms;
using System.Threading;
namespace test
{
public partial class Form1 : Form
{
private bool isRun;
//定义一个委托代理
private delegate void MyInvoke();
public Form1()
{
InitializeComponent();
isRun = false;
//加上这句话后,线程的处理就同vs2003完全一样了,即允许线程间相互调用
//CheckForIllegalCrossThreadCalls = false;
}
private void AddList()
{
while (isRun)
{
MyInvoke mi = new MyInvoke(InvokeAddList);
this.BeginInvoke(mi);
Thread.Sleep(20);
}
}
private void InvokeAddList()
{
listBox1.Items.Add("Working");
}
private void button1_Click(object sender, EventArgs e)
{
isRun=true;
Thread thread1 = new Thread(new ThreadStart(new MethodInvoker(AddList)));
thread1.Name = "StartThread1";
thread1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
isRun = false;
}
}
}
using System;
using System.Windows.Forms;
using System.Threading;
namespace test
{
public partial class Form1 : Form
{
private bool isRun;
//定义一个委托代理
private delegate void MyInvoke();
public Form1()
{
InitializeComponent();
isRun = false;
//加上这句话后,线程的处理就同vs2003完全一样了,即允许线程间相互调用
//CheckForIllegalCrossThreadCalls = false;
}
private void AddList()
{
while (isRun)
{
MyInvoke mi = new MyInvoke(InvokeAddList);
this.BeginInvoke(mi);
Thread.Sleep(20);
}
}
private void InvokeAddList()
{
listBox1.Items.Add("Working");
}
private void button1_Click(object sender, EventArgs e)
{
isRun=true;
Thread thread1 = new Thread(new ThreadStart(new MethodInvoker(AddList)));
thread1.Name = "StartThread1";
thread1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
isRun = false;
}
}
}