zoukankan      html  css  js  c++  java
  • 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;
                }
            }
    
        }
    }

     完整Demo下载

  • 相关阅读:
    python 的csr_python
    OpenBLAS学习一:源码架构解析&GEMM分析
    gcc警告选项汇总
    Welcome! This is the documentation for Python 3.6.8
    CentOS 7 查看和设置防火墙状态
    django中多个app放入同一文件apps
    安装mysqlclient, 链接mysql失败,提示“Library not loaded: @rpath/libmysqlclient.21.dylib”
    动手玩转Docker(二)
    动手玩转Docker(一)
    转载:微服务部署:蓝绿部署、滚动部署、灰度发布、金丝雀发布
  • 原文地址:https://www.cnblogs.com/runningRain/p/9215233.html
Copyright © 2011-2022 走看看