• WinForm多线程+委托防止界面假死


    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;
                }
            }
    
        }
    }
  • 相关阅读:
    ZooKeeper基本原理
    Ubuntu上部署C# 网站 步骤简单记录
    代码生成助手
    微信授权封装,欢迎使用
    c#微信开发,使用JS-SDK自定义分享功能,分享朋友圈,分享给朋友等
    ab.exe使用
    【分享·微信支付】 C# MVC 微信支付教程系列之公众号支付
    SVN服务器搭建(一)
    MVC四大筛选器—ActionFilter&ResultedFilter
    MySQL参数化查询的IN 和 LIKE
  • 原文地址:https://www.cnblogs.com/DoNetCShap/p/7263308.html
Copyright © 2020-2023  润新知