码迷,mamicode.com
首页 > 编程语言 > 详细

执行线程,不卡死主线程

时间:2014-11-20 01:18:27      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:io   ar   os   on   art   cti   ad   new   tt   

private void button1_Click(object sender, EventArgs e)
{
int i = 0;
Thread th = new Thread(new ThreadStart(() =>
{
while (true)
{
// Thread.Sleep(1000);//繁琐的操作,或者IO等待放在这个线程里面操作
this.Invoke(new Action(() =>
{//当需要操作界面元素时,需要用Invoke,注意这里面不能有繁琐的操作
txtBox.Text = (i++).ToString();
//Thread.Sleep(1000);如果这么写,就会卡住主线程
}));
}
}));
th.IsBackground = true;//一定要标记为后台线程,这样这个线程才能在你的主线程停止后自动停止
th.Start();
}

执行线程,不卡死主线程

标签:io   ar   os   on   art   cti   ad   new   tt   

原文地址:http://www.cnblogs.com/mengyirensheng/p/4109526.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!