进度条未显示填充 (vb.net/Visual Studio)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23371769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:29:02  来源:igfitidea点击:

Progress bar not showing fill (vb.net/Visual Studio)

vb.netvisual-studio-2013

提问by Kat

I have a code set up that will loop around and perform some network firmware functions. For the user, I have a progress bar form that pops up and shows them the status as it happens. The code is pretty straight forward:

我设置了一个代码,可以循环执行一些网络固件功能。对于用户,我有一个进度条表单,它会弹出并在发生时向他们显示状态。代码非常简单:

 ProgressBar.Show()

 For j As Integer = 0 To datagridview.Rows.Count - 1

 'do network functions

 ProgressBar.ProgressBar1.Increment(100 / (datagridview.Rows.Count))

 next

 ProgressBar.Close()

ProgressBar is the name of the form with the progressbar on top of it. It's a very simple form. So if I put a break point at the "for" line, i.e. j = 0, I get my progress bar looking like this: enter image description here

ProgressBar 是表单的名称,上面有进度条。这是一个非常简单的表格。因此,如果我在“for”行放置一个断点,即 j = 0,我的进度条将如下所示: 在此处输入图片说明

Well, that gives no info. If I step to the point of the incrementing line below, it shows! enter image description here

嗯,这没有提供任何信息。如果我走到下面递增线的点,它会显示! 在此处输入图片说明

I thought that if I add some progressbar increment earlier, that the bar would fill. So I tried that. So here's what I changed it to:

我想如果我早点添加一些进度条增量,进度条就会填满。所以我试过了。所以这是我将其更改为:

 ProgressBar.Show()

 ProgressBar.ProgressBar1.Increment(100 / (datagridview.Rows.Count+1))

 For j As Integer = 0 To datagridview.Rows.Count - 1

 'do network functions

 ProgressBar.ProgressBar1.Increment(100 / (datagridview.Rows.Count+1))

 next


ProgressBar.Close()

Yet if I stop at the for loop in a break point, I still have a white bar instead of a green filled one. What am I doing wrong? I feel that this is something simple/noob level. Appreciated.

然而,如果我在断点处停在 for 循环处,我仍然有一个白色的条,而不是一个绿色的填充条。我究竟做错了什么?我觉得这是简单/菜鸟级别的东西。赞赏。

回答by TimG

@Hans Passant's comment is correct. Your UI thread is stuck. It is waiting for your background thread (ie, your looping code) to finish before it repaints the screen.
When I use a progress bar, I put a DoEvents() call in the loop, like this:

@Hans Passant 的评论是正确的。您的 UI 线程卡住了。它正在等待您的后台线程(即您的循环代码)在重新绘制屏幕之前完成。
当我使用进度条时,我在循环中放置了一个 DoEvents() 调用,如下所示:

ProgressBar.Show()

ProgressBar.ProgressBar1.Increment(100 / (datagridview.Rows.Count+1))

For j As Integer = 0 To datagridview.Rows.Count - 1
   'do network functions
   ProgressBar.ProgressBar1.Increment(100 / (datagridview.Rows.Count+1))
   'force .NET to update the forground thread, so the progress bar looks like it should
   Application.DoEvents()
Next

ProgressBar.Close()

The call to DoEvents, essentially halts the background execution until the UI has time to update, and then continues the background execution.

对 DoEvents 的调用实质上是停止后台执行,直到 UI 有时间更新,然后继续后台执行。