会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
My Program life
吴碧宇,Knightfire,Wubiyu,
Google Doc
博客园
首页
新随笔
联系
订阅
管理
c# 多线程基本操作
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Threading;
namespace
winfromDeom
{
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
button2.Enabled
=
false
;
}
//
声名一个线程
public
Thread t;
public
delegate
void
upUI();
private
void
button1_Click(
object
sender, EventArgs e)
{
progressBar1.Value
=
0
;
button1.Enabled
=
false
;
button2.Enabled
=
true
;
//
实列化一个线程
t
=
new
Thread(
new
ThreadStart(doThread));
//
设置是否是后台执行的线程
t.IsBackground
=
true
;
//
开始执行这个线程
t.Start();
}
private
void
doThread()
{
//
更新UI委托
upUI upui
=
new
upUI(upDataUi);
try
{
while
(
true
)
{
Thread.Sleep(
0
);
//
开始更新UI
this
.Invoke(upui);
}
}
finally
{
Thread.Sleep(
5000
);
}
}
private
void
button2_Click(
object
sender, EventArgs e)
{
//
设置为终止状态
t.Abort();
//
阻塞这个线程
t.Join();
button1.Enabled
=
true
;
button2.Enabled
=
false
;
}
/**/
///
<summary>
///
执行UI更新的方法
///
</summary>
private
void
upDataUi()
{
if
(progressBar1.Value
!=
100000
)
{
progressBar1.Value
=
progressBar1.Value
+
10
;
label1.Text
=
progressBar1.Value.ToString();
}
else
{
t.Abort();
t.Join();
button1.Enabled
=
true
;
button2.Enabled
=
false
;
}
}
private
void
Form1_FormClosing(
object
sender, FormClosingEventArgs e)
{
t.Abort();
t.Join();
}
}
}
posted @
2007-07-15 16:50
吴碧宇
阅读(
477
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告