国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

WinForm多線程及委托防止界面假死

2019-11-17 02:44:17
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

WinForm多線程及委托防止界面假死

當(dāng)有大量數(shù)據(jù)需要計(jì)算、顯示在界面或者調(diào)用sleep函數(shù)時(shí),容易導(dǎo)致界面卡死,可以采用多線程加委托的方法解決。

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 WindowsFormsapplication1{ public partial class FormMain : Form { DataTable table; int currentIndex = 0; int max = 10000; public FormMain() { InitializeComponent(); } PRivate void button1_Click(object sender, EventArgs e) { button1.Enabled = false; Thread thread = new Thread(new ThreadStart(LoadData)); thread.IsBackground = true; thread.Start(); progressBar1.Minimum = 0; progressBar1.Maximum = max; } private void LoadData() { SetLableText("數(shù)據(jù)加載中..."); currentIndex = 0; table = new DataTable(); table.Columns.Add("id"); table.Columns.Add("name"); table.Columns.Add("age"); while (currentIndex < max) { SetLableText(string.Format("當(dāng)前行:{0},剩余量:{1},完成比例:{2}%", currentIndex, max - currentIndex, (Convert.ToDecimal(currentIndex) / Convert.ToDecimal(max) * 100).ToString("f0"))); SetPbValue(currentIndex); DataRow dr = table.NewRow(); dr["id"] = currentIndex; string name = "張三"; dr["name"] = name; dr["age"] = currentIndex + 5; table.Rows.Add(dr); currentIndex++; } SetDgvDataSource(table); SetLableText("數(shù)據(jù)加載完成!"); this.BeginInvoke(new MethodInvoker(delegate() { button1.Enabled = true; })); } delegate void labDelegate(string str); private void SetLableText(string str) { if (label1.InvokeRequired) { Invoke(new labDelegate(SetLableText), new string[] { str }); } else { label1.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; } } delegate void pbDelegate(int value); private void SetPbValue(int value) { if (progressBar1.InvokeRequired) { Invoke(new pbDelegate(SetPbValue), new object[] { value }); } else { progressBar1.Value = value; } } }}

運(yùn)行效果圖:

}<br>運(yùn)行效果圖:


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金乡县| 长海县| 河池市| 潮安县| 丰镇市| 颍上县| 永康市| 特克斯县| 寻乌县| 石嘴山市| 河津市| 花垣县| 当雄县| 雷山县| 贡嘎县| 宜良县| 年辖:市辖区| 湘潭县| 柯坪县| 绵竹市| 松江区| 中西区| 登封市| 容城县| 罗田县| 南雄市| 雷山县| 叶城县| 新巴尔虎右旗| 平原县| 若尔盖县| 图们市| 西平县| 临桂县| 肃南| 伊川县| 吉隆县| 屯留县| 连江县| 淅川县| 陕西省|