For one thing - are you using Application.DoEvents in your loops since these
are taking so long? That way the UI has a chance to repaint itself and in
that case you probably wouldn't even have to do refresh on the controls as
long as you are correctly updating the progress bars in your loop as you are
proceeding. A cleaner solution would be create a new thread to do the
processing on your graphics so that the main thread is free. Here's a small
program that I have that will simulate the kind of behaviour you are trying
to achieve:
Imports System.Threading
Public Class Form1
Inherits System.Windows.Forms.Form
Private WorkerThread As New Thread( New ThreadStart( _
AddressOf MyLongMultiLoopProcedure))
Private Sub MyLongMultiLoopProcedure()
Dim lCnt1 As Integer
Dim lCnt2 As Integer
Dim lCnt3 As Integer
For lCnt1 = 1 To Integer.MaxValue
If lCnt1 Mod 10000 = 0 Then
ProgressBar1.Value = lCnt1
End If
For lCnt2 = 1 To Integer.MaxValue
If lCnt2 Mod 10000 = 0 Then
ProgressBar2.Value = lCnt2
End If
For lCnt3 = 1 To Integer.MaxValue
If lCnt3 Mod 10000 = 0 Then
ProgressBar3.Value = lCnt3
End If
Next
Next
Next
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For Each oControl As Control In Me.Controls
If TypeOf oControl Is ProgressBar Then
CType(oControl, ProgressBar).Minimum = 0
CType(oControl, ProgressBar).Maximum = Integer.MaxValue
End If
Next
End Sub
Private Sub Form1_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
If WorkerThread.IsAlive Then
WorkerThread.Abort()
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
WorkerThread.Start()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
WorkerThread.Abort()
End Sub
End Class
I have ommited the form designer code - basically, I've just placed 3
progress bars named progressbar1, progressbar2 and progressbar3 and two
buttons btStart and btStop. btStart will start the new thread which executes
the long running procedure with 3 time consuming loops. btStop will stop
it - obviously
If you run this program, you'll see that the UI never freezes up since the
main thread (running the UI) is free while the new thread that we created is
doing the work. Try calling the procedure directly (just replace
WorkerThread.Start with MyLongMultiLoopProcedure( ) ) and although the
progressbars update, you'll notice that the form does not respond. Also, if
you activate any other application in front of the form, the form stops
responding and nothing appears on the form although ofcourse the process is
still running and you'll know when it finishes
You could do away with adding the line Application.DoEvents in each of the
loops and it should work fine but I think threads would be the way to go for
you.
hope this helps..
Imran.