winform 多线程
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了winform 多线程相关的知识,希望对你有一定的参考价值。
这里有两个按钮,按"启动线程"时,启动一个线程,这个线程的作用是从0开始,每秒累加1,值显示在文本框中,而点"终止线程"时,终止线程,也不再计数了,
本人试过很多办法,要么是UI死掉,要么是线程无法终止而导致无法关闭程序,拜托各位大虾帮帮忙了...在这里先谢过各位了,呵呵
下面是我写的:
public partial class Form1 : Form
Thread thread=null;
int n = 0;
bool severIsRun=false;//用来村示线程是否启动
private delegate void myDelegate(string str);//定义委托
private void setRich(string str)//委托
if (this.textBox1.InvokeRequired)
myDelegate md = new myDelegate(this.setRich);
this.Invoke(md, new object[] str );
else
this.textBox1.Text =str;
public Form1()
InitializeComponent();
private void run()
while(true)
setRich((n++).toString());
Thread.Sleep(1000);
private void button1_Click(object sender, EventArgs e)
if(!severIsRun)
ThreadStart ts =new ThreadStart(run);
Thread = new Thread(ts);
severIsRun=true;
thread.Start();
private void button2_Click(object sender, EventArgs e)
if(severIsRun)
thread.Abort();
severIsRun=false;
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
if(severIsRun)
thread.Abort();
severIsRun=false;
主要是想告诉你,控件是主线程的,子线程要想用必需建立委托,不然会出错
还有一种方法比较简单,但那种不安全,就不说了, 有不明白的你可以找我 参考技术A 看看是不是你要的
public partial class Form1 : Form
Thread thread;
static int n = 0;
public Form1()
InitializeComponent();
private void run()
while(true)
textBox1.Text = Convert.ToString(n++);
Thread.Sleep(1000);
private void button1_Click(object sender, EventArgs e)
thread = new Thread(new ThreadStart(run));
thread.Start();
private void button2_Click(object sender, EventArgs e)
try
if (thread.IsAlive)
thread.Abort();
catch
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
button2_Click(sender, (EventArgs)e);
参考技术B ls,这道题根本不需要委托,主线程并没有用到textbox,1L的程序就可以了,不是说只要多线程就要委托,这种简单的题目没有必要,只是增加程序复杂度而已 参考技术C 顺便加一句 Thread t = new Thread(method);
t.IsBackground = true;//设为后台线程,在主程序关闭的结束未终止的线程
t.Start(); 参考技术D 同意 “hen总受伤的我” 的,子线程调用主线程控件需要用委托
WinForm 多线程+委托来防止界面假死
参考: http://www.cnblogs.com/xpvincent/archive/2013/08/19/3268001.html
当有大量数据需要计算、显示在界面或者调用sleep函数时,容易导致界面卡死,可以采用多线程加委托的方法解决;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsFormsApplication3 { public partial class Form1 : Form { DataTable table; int currentIndex = 0; int max = 10000; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { buttonOK.Enabled = false; Thread thread = new Thread(new ThreadStart(LoadData)); thread.IsBackground = true; thread.Start(); progressBar1.Minimum = 0; progressBar1.Maximum = max; } private void LoadData() { SetLabelText("数据加载中..."); currentIndex = 0; table = new DataTable(); table.Columns.Add("id"); table.Columns.Add("name"); table.Columns.Add("age"); while (currentIndex < max) { SetLabelText(string.Format("当前行:{0},剩余量:{1},完成比例:{2}%", currentIndex, max - currentIndex, (Convert.ToDecimal(currentIndex) / Convert.ToDecimal(max) * 100).ToString())); SetPbValue(currentIndex); DataRow dr = table.NewRow(); dr["id"] = currentIndex; dr["name"] = "张三"; dr["age"] = currentIndex + 5; table.Rows.Add(dr); currentIndex++; } SetDgvDataSource(table); SetLabelText("数据加载完成"); this.BeginInvoke(new MethodInvoker(delegate() { buttonOK.Enabled = true; })); } delegate void labDelegate(string str); private void SetLabelText(string str) { if (textBox1.InvokeRequired) { Invoke(new labDelegate(SetLabelText), new string[] { str }); } else { textBox1.Text = str; } } delegate void dgvDelegate(DataTable table); private void SetDgvDataSource(DataTable table) { if (dataGridView1.InvokeRequired) { Invoke(new dgvDelegate(SetDgvDataSource), new object[] { table }); } else { dataGridView1.DataSource = table; } } private delegate void pbDelegate(int value); private void SetPbValue(int value) { if (progressBar1.InvokeRequired) { Invoke(new pbDelegate(SetPbValue), new object[] { value }); } else { progressBar1.Value = value; } } } }
以上是关于winform 多线程的主要内容,如果未能解决你的问题,请参考以下文章