数据库加载时带进度条功能
这是我在网上找的资料做的一个加载数据库的进度条,请大家看看可以怎么优化?
我的问题backgroundWorker的时间是由argument和Thread.Sleep(20);来控制的这样有一个问题就是会出现数据库没有加载完成但是进度条满了.如何让进度条更准确点.
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using ClientManage.BLL;
namespace ClientManage
{
public partial class Form4 : Form
{
private DataView dv_Gift;
const int argument = 2000;
public Form4()
{
InitializeComponent();
this.progressBar1.Maximum = 100;
this.progressBar1.Minimum = 0;
this.progressBar1.Step = 1;
this.Dgv_Gift.SetColumnsFromModule(Class.ModuleCode.GiftList1);
this.textBox1.Text = "select top 100000 * FROM V_Gift_List";
}
private void ThSetData()
{
// new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
ClientManage.BLL.T_Gift giftBll = new T_Gift();
this.dv_Gift = giftBll.GetListTop(this.textBox1.Text.Trim()).Tables[0].DefaultView;
foreach (DataRow row in this.dv_Gift.Table.Rows)
{
row["TrueName"] = DESEncrypt.Decrypt(row["TrueName"].ToString());
row["Phone"] = DESEncrypt.Decrypt(row["Phone"].ToString());
}
this.backgroundWorker1.CancelAsync();
}
private void button1_Click(object sender, EventArgs e)
{
//new System.Threading.Thread(new System.Threading.ThreadStart(ThSetData)).Start();
Thread thread = new System.Threading.Thread(new ThreadStart(ThSetData));
thread.IsBackground = true;
thread.Start();
if (this.backgroundWorker1.IsBusy)
{
MessageBox.Show("慢则那,等着吧");
return;
}
this.backgroundWorker1.RunWorkerAsync(argument);
this.button2.Enabled = true;
//thread.Join();
//this.backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//for (int i = 0; i < int.Parse(e.Argument.ToString()); i++)
//{
// e.Result = i;
// (sender as BackgroundWorker).ReportProgress((int)(((double)i / (double)((int)e.Argument)) * 100), i);
// // percent = (int)(((double)i / (double)maxRecords) * 100);
// //worker.ReportProgress(percent, new KeyValuePair<int, string>(i, Guid.NewGuid().ToString()));
// Thread.Sleep(20);
// if (this.backgroundWorker1.CancellationPending)
// return;
//}
try
{
e.Result = this.RetrieveData(this.backgroundWorker1, e);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
throw;