Progress Bar counting problems......
With the help of others here I was able to get some code working to create a dummy file to fill the remaining space of a flash drive. However, I have added a progress bar and can't figure out why it's not working. I declare a variable for the current value, min, & max properities of the progressbar and then increment it by 1 during each step of my FOR loop. However, I never see anything on the progress bar. Thanks for any help.
Code:
Dim CurVal As Integer
Dim RandomNumber As Byte
'Create the dummyfile to take up the remaining space on the drive
Dim myStreamWriter As New IO.StreamWriter("C:\dummyfile.txt")
dumProgBar.Minimum = 1
dumProgBar.Maximum = RemSpace
CurVal = 0
For i As Integer = 1 To RemSpace
RandomNumber = CByte(RandomClass.Next(0, 256))
myStreamWriter.BaseStream.WriteByte(RandomNumber)
dumProgBar.Value = CurVal + 1
Next
myStreamWriter.Close()
myStreamWriter.Dispose()
myStreamWriter = Nothing
-Jeremy
Re: Progress Bar counting problems......
Ahhh............i am not incrementing CurVal. Duh!
Re: Progress Bar counting problems......
That's because the program is too busy running the loop and it doesn't have a chance to update the UI. Proper way to do this is to run the long running task in a different thread. A quick fix is to stick application.DoEvents() in the loop. You want to call Application.DoEvents sparely though, instead of every iteration of the loop. So you can call it like once every 100 iterations or so. Put this in your loop and see what happens.
Code:
If i Mod 100 = 0 Then
Application.DoEvents()
End If
Re: Progress Bar counting problems......
Hmm.... still having issues after changing to this. Still nothing on the progress bar.
Code:
Dim CurVal As Integer
Dim RandomNumber As Byte
'Create the dummyfile to take up the remaining space on the drive
Dim myStreamWriter As New IO.StreamWriter("C:\dummy.txt")
dumProgBar.Minimum = 1
dumProgBar.Maximum = RemSpace
CurVal = 0
For i As Integer = 1 To RemSpace
RandomNumber = CByte(RandomClass.Next(0, 256))
myStreamWriter.BaseStream.WriteByte(RandomNumber)
CurVal = CurVal + 1
dumProgBar.Value = CurVal
Next
myStreamWriter.Close()
myStreamWriter.Dispose()
myStreamWriter = Nothing
Re: Progress Bar counting problems......
Re: Progress Bar counting problems......
That took care of it! thanks for the tip.