Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cancelling

Status
Not open for further replies.

Ladyhawk

Programmer
Jan 22, 2002
534
0
0
AU
I have a process that takes a fair while to complete. I have put a cancel button on the form which has the progress bar for the process. Then basically throughout the process, I check to see if the cancel button has been clicked by refreshing the button and calling a doevents. Clicking on the cancel button sets a property on the form that my process looks for. The problem I have is that the doevents takes ages. Is there a better way of doing this?

Ladyhawk. [idea]
** ASP/VB/Java Programmer **
 
Hi Ladyhawk,

Is the doevent inside the cancel button ?
 
You should reduce the frequency of DoEvents calls. For example if your code executes in a loop then you should call DoEvents, for example once in 1000 iterations, like this.
___


...

For ... ' Start of the loop that does the process

...

'At the end of the loop
Dim Count As Integer
Count = Count + 1
If Count = 1000 Then
Count = 0
DoEvents 'Called once in 1000 iterations.
End If
Next
___

You should also not call the Refresh method of any object (like form or button). When you call a DoEvents, all objects are repainted and refreshed. An additional call to refresh will only waste the time.

Moreover, you should not update the progress bar value during each iteration of the loop. Progress bar should be updated only when it needs to be updated. (i.e. when the new calculated value is different from its current value.)

When you assign a new value to the progress bar's Value property, it is repainted completely even if the new value is same as its current value.

So it is advised to store the current value of the status bar in a variable (0 to 100), calculate the new value, if it is different from the last one stored in the variable, then only assign this new value to progress bar and also update the last value stored in the variable.

Place two command buttons and a progress bar on the form and paste the following code and run the program. You will understand what I mean to say.
___

Private Sub Command1_Click()
Dim N As Long
For N = 1 To 1000000 'Always update the progress bar
ProgressBar1.Value = N / 10000
Next
End Sub

Private Sub Command2_Click()
Dim N As Long, Value As Long, LastValue As Long
For N = 1 To 1000000
Value = N / 10000
If LastValue <> Value Then 'Update only when needed
LastValue = Value
ProgressBar1.Value = Value
End If
Next
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top