在界面有一个button,从SQL里取数据到dataGridView1,由于时间较长,我在界面的下面添加了一个toolStripProgressBar1控件,我知道要显示时时查询数据很难,我就打算做一个假像:
当查询数据时,执行下面的方法,让toolStripProgressBar1不断递增,当到达99时,又从0开始循环,代码如下:
private void SetToolStripProgressBar() //设置进度条显示
{
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Minimum = 0;
toolStripProgressBar1.Maximum = 100;
toolStripProgressBar1.Step = 1;
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50)
toolStripProgressBar1.Value ++;
}
}问题:
1、如何使用查询数据与进度条显示同步进行?
2、如何判断到数据查询完成之后,隐藏进度条?据说要采用异步的方法,才可以使执行数据查询与进度条显示同步进行,但试过不行,才上来求助!
当查询数据时,执行下面的方法,让toolStripProgressBar1不断递增,当到达99时,又从0开始循环,代码如下:
private void SetToolStripProgressBar() //设置进度条显示
{
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Minimum = 0;
toolStripProgressBar1.Maximum = 100;
toolStripProgressBar1.Step = 1;
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50)
toolStripProgressBar1.Value ++;
}
}问题:
1、如何使用查询数据与进度条显示同步进行?
2、如何判断到数据查询完成之后,隐藏进度条?据说要采用异步的方法,才可以使执行数据查询与进度条显示同步进行,但试过不行,才上来求助!
public delegate void ShowProgressDelegate(int minStep, int totalStep, int currentStep);
2.添加一个ProgressBar(进度条)
3. this.tsProgressBar.Visible = true;
this.tsProgressBar.Minimum = 1;
this.tsProgressBar.Maximum = 100;
4.在多线程里面处理
//生成产品条码
Thread _thread = new Thread(new ThreadStart(DoCreateProBarCodeForm));
_thread.IsBackground = true;
_thread.Start();public void DoCreateProBarCodeForm()
{
//进度条委托事件
ShowProgressDelegate showProgress = new ShowProgressDelegate(ShowProgress);
int i=1;
foreach(..)
{
// 显示进度条
this.Invoke(showProgress, new object[] { 1, 100, i });
i++;
}
}
/// <summary>
/// 显示进度条
/// </summary>
/// <param name="minStep">最小值 </param>
/// <param name="totalStep">最大值 </param>
/// <param name="currentStep">当前值 </param>
void ShowProgress(int minStep, int totalStep, int currentStep)
{
this.panelProcess.Visible = true;
this.tsProgressBar.Minimum = minStep;
this.tsProgressBar.Maximum = totalStep;
this.tsProgressBar.Value = currentStep; this.tsProcessLblValue.Text = 100 * currentStep / totalStep + "%";
}
2.在线程里是不能直接访问进度条的控件,那么你也必须申明一个委托
public delegate void CreateBarCodeCompleteDelegate();;
在程序的循环后
CreateBarCodeCompleteDelegate _CreateBarCodeCompleteDelegate = new CreateBarCodeCompleteDelegate(finishSelect)
this.Invoke(_CreateBarCodeCompleteDelegate, new object[] { });
public void finishSelect()
{
//隐藏进度条停止其继续变化运行!
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} delegate void ProgressBarDelegate(int min,int max,int cur); private void button1_Click(object sender, EventArgs e)
{
ProgressBarDelegate pbd = new ProgressBarDelegate(ShowPregressBar) ;
for (int i = 1; i <= 100;i++ )
{
this.Invoke(pbd, new object[] { 1, 100, i });
System.Threading.Thread.Sleep(100);
}
} private void Form1_Load(object sender, EventArgs e)
{
}
private void ShowPregressBar(int min, int max ,int cur)
{
pb.Maximum = max;
pb.Minimum = min;
pb.Value = cur;
}
}
}
查询数据就简单:
private void button1_Click(object sender, EventArgs e)
{
string sqlStr="SELECT * FROM TABLE";
SqlDBHelper GetSqlDataSet = new SqlDBHelper();
DateSet ds = (DataSet)GetSqlDataSet.GetDataSet(sqlStr, CommandType.Text);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}然后需要这个执行过程中,让进度条也不断的递增,到达100时又返回从0开始,进度条的方法:
private void SetToolStripProgressBar() //设置进度条显示
{
toolStripProgressBar1.Value = 0;
toolStripProgressBar1.Minimum = 0;
toolStripProgressBar1.Maximum = 100;
toolStripProgressBar1.Step = 1;
while (toolStripProgressBar1.Value < 100)
{
if (toolStripProgressBar1.Value == 99)
{
toolStripProgressBar1.Value = 0;
}
Thread.Sleep(50)
toolStripProgressBar1.Value ++;
}
}
我现在要么先执行显询再显示进度条,要么先执行进度条,查询就无法执行。我需要的是二者同时执行,并且当数据查询出来之后,结束进度条的运行并隐藏!