|
-
Oct 26th, 2006, 06:11 PM
#1
Thread Starter
Frenzied Member
Why does DoEvents use 100% cpu? And whats an alternative?
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
VB 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
Basically I just want to be able to use
VB Code:
RunSomeFunctionInTheBackground()
waitUntilThatFunctionisDone() '(but not use 100% cpu like doevents, or halt the execution of that function with sleep)
continue with app
Last edited by VaxoP; Oct 27th, 2006 at 02:29 AM.
-
Oct 26th, 2006, 06:26 PM
#2
Addicted Member
Re: Why does DoEvents use 100% cpu? And whats an alternative?
Use a Timer
-
Oct 26th, 2006, 07:11 PM
#3
Re: Why does DoEvents use 100% cpu? And whats an alternative?
Have a look at this post by Jacob Roman. There are a couple of alternatives.
http://www.vbforums.com/showthread.php?t=315416
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Oct 26th, 2006, 07:22 PM
#4
Re: Why does DoEvents use 100% cpu? And whats an alternative?
DoEvents lets Windows handle it's message queue for your program's process. You use it to let your program's windows and controls paint and response.
Sleep will halt the execution of the process for given amount of milliseconds.
As such there is nothing wrong with either; if you want, you may use GetQueueStatus API or whatever it was named to optimize DoEvents calls so that DoEvents isn't called when there is no messages to process. Sleep will give time for other programs, thus eliminating the 100% processor usage. So you can very well use:
VB Code:
Do Until Condition: DoEvents: Sleep 1: Loop
However, if this condition is not from an external program but from within your own program, you might want to consider another kind of coding approach.
-
Oct 26th, 2006, 07:26 PM
#5
Re: Why does DoEvents use 100% cpu? And whats an alternative?
 Originally Posted by Merri
DoEvents lets Windows handle it's message queue for your program's process. You use it to let your program's windows and controls paint and response.
Sleep will halt the execution of the process for given amount of milliseconds.
As such there is nothing wrong with either; if you want, you may use GetQueueStatus API or whatever it was named to optimize DoEvents calls so that DoEvents isn't called when there is no messages to process. Sleep will give time for other programs, thus eliminating the 100% processor usage. So you can very well use:
VB Code:
Do Until Condition: DoEvents: Sleep 1: Loop
However, if this condition is not from an external program but from within your own program, you might want to consider another kind of coding approach.
I was going to recommend GetInputState but I didn't know about the other API.
-
Oct 26th, 2006, 11:26 PM
#6
Thread Starter
Frenzied Member
Re: Why does DoEvents use 100% cpu? And whats an alternative?
the code that was linked is no good, it seriously slows down my machine.. i cant move my mouse or anything. so that didnt work
honestly the best way i can think of doing this is a timer..
VB 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
There HAS to be a more elegant way..
Basically I just want to be able to use
VB Code:
RunSomeFunctionInTheBackground()
waitUntilThatFunctionisDone() '(but not use 100% cpu like doevents, or halt the execution of that function with sleep)
continue with app
Last edited by VaxoP; Oct 26th, 2006 at 11:30 PM.
-
Oct 26th, 2006, 11:47 PM
#7
Hyperactive Member
Re: Why does DoEvents use 100% cpu? And whats an alternative?
 Originally Posted by DigiRev
I was going to recommend GetInputState but I didn't know about the other API. 
Don't use GetInputState in this kind of situation. This API call only polls the "calling thread's message queue"--meaning your program--and not the main/OS-based message queue--which is what DoEvents handles.
I've always preferred using SleepEx over Sleep because it is interruptible--meaning if something happens your program can get working again without having to wait for its timer to end. It also lets the OS do its thing in a simpler/more efficient manner because you are basically telling the OS "Stop my program and do whatever you want to do 'til I need the CPU again".
-
Oct 26th, 2006, 11:51 PM
#8
Hyperactive Member
Re: Why does DoEvents use 100% cpu? And whats an alternative?
 Originally Posted by VaxoP
There HAS to be a more elegant way..
Basically I just want to be able to use
VB Code:
RunSomeFunctionInTheBackground()
waitUntilThatFunctionisDone() '(but not use 100% cpu like doevents, or halt the execution of that function with sleep)
continue with app
There is a way to do it, but you can't do it in the same thread. It sounds like you want a VB version of C's fork() which is async. processing, and when it is done, you continue with the parent program. The only way I think you're going to be able to do that is use an ActiveX EXE or a separate program/process to crunch on your data and raise an event to your main/parent program that it can proceed.
-
Oct 27th, 2006, 04:37 AM
#9
Re: Why does DoEvents use 100% cpu? And whats an alternative?
Not sure if this is what you're after, but if you're not calling an external app then you may be able to use an "off-screen" textbox's Change event. It's a kludge but works. Something like this:
VB Code:
Option Explicit
Dim intChange As Integer 'Text change flag. Use boolean in a real app.
Private Sub Form_Click()
RunSomeFunctionInTheBackground
End Sub
Private Function RunSomeFunctionInTheBackground() As String
'do something that takes 10 seconds
'Blah, blah
'etc, etc
'Ok, time to exit
RunSomeFunctionInTheBackground = "My output"
'Change Text1.
If intChange = 1 Then
Text1.Text = "Finished"
intChange = 2
Else
Text1.Text = "Over and out"
intChange = 1
End If
End Function
Private Sub Form_Load()
intChange = 1
End Sub
Private Sub Text1_Change()
MsgBox "All done"
'Carry on...
Continue_With_App
End Sub
Public Sub Continue_With_App()
'Blah, blah
'etc, etc
End Sub
-
Oct 27th, 2006, 04:49 AM
#10
Frenzied Member
Re: Why does DoEvents use 100% cpu? And whats an alternative?
with the text box change....
if you have
VB Code:
private sub Textbox_change()
for i=0 to 100
me.print i
next i
end sub
private mysub()
'do some stuff
textbox.text="xox"
'do some more stuff
end sub
is it that the more stuff happens whilst 1 - 100 is printed? if not then why not just call the sub at the end of the other function?
sorry, just thinking about it, but what are you doin? - maybe a better way? :S ta
-
Oct 27th, 2006, 04:58 AM
#11
Re: Why does DoEvents use 100% cpu? And whats an alternative?
True. My bad. VB is (normally) single threaded. Not quite sure what VaxoP is after.
EDIT: Ah, now I do... (multi threading)
Google for "multi thread visual basic". You'll get about 10,000,000 hits with topics such as Intro to Multi-Threading in VB
Last edited by schoolbusdriver; Oct 27th, 2006 at 05:44 AM.
-
Oct 27th, 2006, 06:46 AM
#12
Frenzied Member
Re: Why does DoEvents use 100% cpu? And whats an alternative?
 Originally Posted by schoolbusdriver
True. My bad. VB is (normally) single threaded. Not quite sure what VaxoP is after.
EDIT: Ah, now I do... (multi threading)
Google for "multi thread visual basic". You'll get about 10,000,000 hits with topics such as Intro to Multi-Threading in VB
COOL!
cheers... really helpful for me too - never realised that adding a thread was so easy...
-
Oct 27th, 2006, 06:58 AM
#13
Re: Why does DoEvents use 100% cpu? And whats an alternative?
 Originally Posted by wpearsall
COOL!
cheers... really helpful for me too  - never realised that adding a thread was so easy...
Then don't forget to rate me
-
Oct 28th, 2006, 02:00 AM
#14
Thread Starter
Frenzied Member
Re: Why does DoEvents use 100% cpu? And whats an alternative?
so if i were to run my background process in another thread using multithreading.. how can i pause the main app until the background execution is done?
-
Oct 28th, 2006, 04:48 AM
#15
Re: Why does DoEvents use 100% cpu? And whats an alternative?
VaxoP,
Just for the record... DoEvents doesn't use 100% of your CPU your loop does. You are blaming DoEvents for bad coding...
DoEvents allows other processes to execute. Take DoEvents out of your loop and it will still take 100% of the CPU so what does DoEvents have to do with it?
If coded correct you would have your code in an event since VB is event driven and in that way nothing would happen until the event triggered. Your code that you have eecuting could trigger the event itelf. In this way your code would alway be in a wait state until something triggered it to do something else.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|