博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WinForm多线程+委托防止界面假死
阅读量:5752 次
发布时间:2019-06-18

本文共 2857 字,大约阅读时间需要 9 分钟。

当有大量数据需要计算、显示在界面或者调用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 MutiThread{    public partial class Form1 : Form    {        DataTable table;        int currentIndex = 0;        int max = 10000;                public Form1()        {            InitializeComponent();        }        private void buttonOK_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 (label1.InvokeRequired)            {                Invoke(new labDelegate(SetLabelText), 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;            }        }        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;            }        }    }}

程序运行界面如下:

转载地址:http://ugzkx.baihongyu.com/

你可能感兴趣的文章
Spring boot 整合CXF webservice 全部被拦截的问题
查看>>
Pinpoint跨节点统计失败
查看>>
【Canal源码分析】Canal Server的启动和停止过程
查看>>
机房带宽暴涨问题分析及解决方法
查看>>
iOS 绕过相册权限漏洞
查看>>
我的友情链接
查看>>
XP 安装ORACLE
查看>>
八、 vSphere 6.7 U1(八):分布式交换机配置(vMotion迁移网段)
查看>>
[转载] 中华典故故事(孙刚)——19 万岁
查看>>
修改hosts文件里面的主机名,oralce asm无法启动
查看>>
Maven学习总结(十)——使用Maven编译项目gbk的不可映射问题
查看>>
php5编译安装常见错误和解决办法集锦
查看>>
Linux远程访问及控制
查看>>
MongoDB实战系列之五:mongodb的分片配置
查看>>
Unable to determine local host from URL REPOSITORY_URL=http://
查看>>
Java Tomcat SSL 服务端/客户端双向认证(二)
查看>>
java基础(1)
查看>>
ORACLE配置,修改tnsnames.ora文件实例
查看>>
用户无法在输入框中键入数字
查看>>
Workstation服务无法启动导致无法访问文件服务器
查看>>