进度条没有进展 [英] progress bar not progressing

查看:87
本文介绍了进度条没有进展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序正在更新sqlserver中的表,我有一个表格要显示其进度,进度栏正在增加,但未显示.我需要为此使用背景工作人员吗?我在做什么的例子

I have a program which is updating a table in sqlserver, I have a form which I want to show the progress of this, the progress bar is incrementing but this is not being displayed. do i need to use background worker for this? example of what im doing

public void updateTable(string tableName)
{
  // con is an instance of my form to access progressbar
  con.progressBar1.Minimum = 1;
  con.progressBar1.Step = 1;
  string dbQuery = "select summet from someting"

  con.progressBar1.Maximum = address.Tables[0].Rows.Count;
  MessageBox.Show("progress bar max " + con.progressBar1.Maximum);

   foreach (DataRow LonLat in address.Tables[0].Rows)
   {
       con.progressBar1.PerformStep();
       MessageBox.Show(con.progressBar1.Value.ToString()); // this is incrementing
       //plus updating table 

   }


}

推荐答案

简短答案,是的.如果从主线程执行此操作,则不会看到进度条更新.

Short answer, yes. If you do it from the main thread you won't see the progress bar update.

详细答案:

无论何时,只要将您的一段代码中的代码修改为消息",然后将其修改为窗口消息队列中的用户界面,该消息队列就会获取该消息并相应地在主线程中更新UI.

Whenever you modify the user interface from a piece of your code that's translated into a "message" into the window message queue that will get that message and update the UI accordingly in the main thread.

但是,由于主线程正忙于处理您的代码,因此它没有时间"来实际更新用户界面,只有在您的过程完成后,才可以自由更新用户界面.这就是为什么您看到进度条从0%变为100%而没有任何中间步骤的原因.

However, since the main thread is busy handling your piece of code it doesn't have the "time" to actually update the user interface, and only when your process is done, it's free to update the user interface. That's why you see the progress bar going from 0% to 100% without any intermediate steps.

您应该做什么:

您想要做的是将工作放到后台工作人员中,这样,主线程就可以自由地参加UI更新请求……顺便说一句,这是您想要保持UI响应能力的一种标准做法.

What you wanna do is put the work into a background worker, that way the main thread is free to attend UI update requests... that's, by the way, a standard practice if you wanna keep the UI responsive.

这篇关于进度条没有进展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆