why progressbar freezes the main form?

iknowyou

Active member
Joined
Jun 14, 2011
Messages
37
Programming Experience
1-3
VB.NET:
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ProgressBar1.Value = 0
        ProgressBar1.Maximum = 10000000
        For i As Integer = 0 To 10000000
            ProgressBar1.Value = i
        Next
    End Sub
 
You need to insert an Application.DoEvents() like so:

ProgressBar1.Value = 0
ProgressBar1.Maximum = 10000000
For i As Integer = 0 To 10000000
    ProgressBar1.Value = i
    Application.DoEvents()
Next


If you are just trying to make a progress bar go slow you might consider using something more like this below or using a timer control, it will make your timing more accurate as well as consuming less system resources.

ProgressBar1.Value = 0
ProgressBar1.Maximum = 1000
For i As Integer = 0 To 1000
    ProgressBar1.Value = i
    System.Threading.Thread.Sleep(50)
    Application.DoEvents()
Next
 
Back
Top