您的位置:首页 > 其它

使用委托及控件的invoke方法处理窗体控件的跨线程访问

2015-06-02 08:27 519 查看
namespace thread2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread threadb;

delegate void setText(string s);
setText ST;

private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "0";
ST = new setText(setLabelText);

}

private void setLabelText(string s)
{
label1.Text = s;
}

private void button1_Click(object sender, EventArgs e)
{
threadb = new Thread(new ThreadStart(runner));
threadb.Start();
}

private void runner()
{
for (int i = 0; i <= 100; i++)
{
label1.Invoke(ST, new object[]{i.ToString()});
Thread.Sleep(500);
}
}
private void thread2_FormClosing(object sender, FormClosingEventArgs e)
{
if (threadb.IsAlive)
threadb.Abort();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: