zoukankan      html  css  js  c++  java
  • 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.Data.SqlClient;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Globalization;
    using System.Web;
    
    
    namespace Redis_AddData
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                label1.Text = "";
            }
            int maxSize = 0;  //16753
            int minSize = 0;
            int currentIndex = 0;
            public static string tableName = "FA";
    
            private void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
    
                Thread trs = new Thread(new ThreadStart(dofor));
                trs.IsBackground = true;
                trs.Start();
    
                progressBar1.Minimum = minSize;
    
                string sql = "select count(id) from tablea ";
                maxSize = Convert.ToInt32(databind.GetSingle(sql));
                progressBar1.Maximum = maxSize;
            }
    
            public void dofor()
            {
                SetLableText("数据加载中...");
                currentIndex = minSize;
                string str = "";
                string sql = "select * from tablea";
    
                SqlDataReader dr = databind.GetExecuteReader(sql, false);
                DataTable Mydt = attribute.GetTableSchema();
                while (dr.Read())  //读取所有记录
                {
                    CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;  //设置indexof  不区分大小写
                    #region 设置显示的标签进度
                    SetLableText(string.Format("当前行:{0},剩余量:{1},完成比例:{2}%", currentIndex, maxSize - currentIndex,
    
                     (Convert.ToDecimal(currentIndex - minSize) / Convert.ToDecimal(maxSize - minSize) * 100).ToString("f0")));
    
                    SetPbValue(currentIndex);
                    #endregion
                  
                   ...
    
                        this.BeginInvoke(new MethodInvoker(delegate()
                        {
    
                            button1.Enabled = false;
                            textBox1.Text = str;
                        }));
    
                    }
                    currentIndex++;
                }
    
                attribute.BulkToDB(Mydt);
    
                this.BeginInvoke(new MethodInvoker(delegate()
                {
    
                    button1.Enabled = true;
                    textBox1.Text = "完成";
                }));
    
            }
            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 pbDelegate(int value);
    
            private void SetPbValue(int value)
            {
    
                if (progressBar1.InvokeRequired)
                {
    
                    Invoke(new pbDelegate(SetPbValue), new object[] { value });
    
                }
    
                else
                {
    
                    progressBar1.Value = value;
    
                }
    
            }
        }
    }
  • 相关阅读:
    在Eclipse或者STS中使用SVN插件
    SVN的忽略、版本回退、版本冲突(window)
    SVN的图标集
    HTML5+NodeJs实现WebSocket即时通讯
    import和require的区别
    正则表达式 金额验证
    vue 弹窗弹窗禁止滑动的方法 看了网上很多方法 都是扯淡 直接贴上代码
    uni-app关于小程序及app端第三方微信登陆问题
    用 async/await 来处理异步
    微信小程序 子组件调用父组件方法
  • 原文地址:https://www.cnblogs.com/alaric888/p/4942987.html
Copyright © 2011-2022 走看看