找传奇、传世资源到传世资源站!

C# 多线程(入门级示例)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

后台都线程实例
C# 多线程(入门级示例) C#语言基础-第1张from clipboard public partial class 多线程和委托 : Form { public 多线程和委托() { InitializeComponent(); } Thread drawThread = null; delegate void drawDelegate(int i); //开启子线程 private void btnTest_Click(object sender, EventArgs e) { closeThread(); drawThread = new Thread(new ThreadStart(draw)); drawThread.IsBackground = true; drawThread.Start(); } //draw子线程,循环调用test函数,并且等待一会 private void draw() { try { for (int i = 0; i < 100000000; i ) { test(i); Thread.Sleep(100); } } catch (System.Exception e1) { return; } closeThread(); } //test函数,向textBox中添加数据 private void test(int i) { if (textBox1.InvokeRequired) { drawDelegate d = new drawDelegate(test); Invoke(d, new object[] { i }); } else { textBox1.AppendText(i.ToString() "\r\n"); } } //结束子线程 private void closeThread() { if (drawThread != null) { if (drawThread.IsAlive) { drawThread.Abort(); } } } //窗体关闭时,关闭子线程 private void Form1_FormClosing(object sender, FormClosingEventArgs e) { closeThread(); } private void button1_Click(object sender, EventArgs e) { closeThread(); drawThread = new Thread(new ThreadStart(draw)); drawThread.IsBackground = true; drawThread.Start(); } private void 多线程和委托_FormClosing(object sender, FormClosingEventArgs e) { closeThread(); }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复