博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
winform开线程,避免页面假死
阅读量:5036 次
发布时间:2019-06-12

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

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;            }        }    }}

 

转载于:https://www.cnblogs.com/alaric888/p/4942987.html

你可能感兴趣的文章
JavaScript 对象
查看>>
原生js轮播图(面向对象)
查看>>
数据分析软件及spss简单操作
查看>>
自定义通信协议
查看>>
Unity3d--Space Shooter(官方教程)--学习感想(3)
查看>>
java中Collections.sort()方法实现集合排序
查看>>
nodejs笔记之事件循环
查看>>
JVM之垃圾收集器
查看>>
Windows下R画图举例
查看>>
php-fpm 重启 nginx单独配置 重启
查看>>
JS正则表达式RegExp 对象
查看>>
Springboot
查看>>
go语言之进阶篇值语义和引用语义
查看>>
go语言之进阶篇无缓冲channel
查看>>
linux 常见命令
查看>>
func_get_args 笔记
查看>>
hdu 2881(LIS变形)
查看>>
关于break,continue,goto,return语句区别详解(所有语言通用的语法知识)
查看>>
性能测试初级篇1(理论知识)
查看>>
ServletConfig与ServletContext
查看>>