Do
Doevents
Loop until condition = true
thats the code thats generally used to have the app perform a task 'synchronously' so that further code is not executed until something else is done.
But the problem is, the loop causes 100% cpu usage until the condition is true, slowing all other open processes needlessly.
An alternative is adding the sleep function to the loop
Do
Doevents
Sleep 100
Loop until condition = true
but Sleep causes all executions to halt in the app![]()
so..
Doevents: Lets the app execute the background function, but uses 100% cpu
Sleep: Does not let the app execute the background function, uses 0% CPU
Combination: App can execute things in the background while sleep is not being called, ~0% CPU, but not good enough.
There HAS to be a better way to wait until something is done (background) before going on to the next line, without using doevents or sleep..
Anyone know what that is?
the best way i can think of doing this is a timer.. but its not elegant at all
Basically I just want to be able to useVB Code:
'below is an alternative to RunSomeFunctionInTheBackground(): Do: Doevents: Loop until condition = true: Continue with other stuff Private Sub Form_Load() mySub 0 End Sub Private Sub mySub(whereToStart As Integer) Select Case whereToStart Case 0 RunSomeFunctionInTheBackground() Timer1.Enabled = True Exit Sub Case 1 'The function is done running, I can continue with my app End Select End Sub Private Sub Timer1_Timer() If someCondition = True Then Timer1.Enabled = False mySub 1 End If End Sub 'this method will function like DoEvents, and it will not halt execution of the thread like Sleep. Timer interval is something like 10
VB Code:
RunSomeFunctionInTheBackground() waitUntilThatFunctionisDone() '(but not use 100% cpu like doevents, or halt the execution of that function with sleep) continue with app




Reply With Quote