-
Mar 19th, 2008, 05:25 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] VB6 Wait for condition
How can i make my VB6 app to wait for a condition but not to use much of CPU?
-
Mar 19th, 2008, 05:30 PM
#2
Addicted Member
Re: VB6 Wait for condition
System.Threading.Thread.Sleep(miliseconds)
-
Mar 19th, 2008, 06:01 PM
#3
Re: VB6 Wait for condition
Originally Posted by Dungeon Keeper
How can i make my VB6 app to wait for a condition but not to use much of CPU?
That's kind of a loaded question. What condition, can you be more specific? For example, shelling out a command prompt and waiting: easy. Waiting for a specific time of day: easy. But they use different methods.
-
Mar 19th, 2008, 09:29 PM
#4
Re: VB6 Wait for condition
I guess he's wanting an alternative to:
Code:
Do
DoSomething in the meantime
Loop Until Condition = True
Which seems to use as much CPU is available at the time. I'd be interested in hearing of alternative methods. I think some people use an API timer and sometimes a regular timer which is less CPU intensive.
For launching a process I think there is WaitForObject (or whatever it is) but I'm quickly getting into areas I don't know anything about...
-
Mar 19th, 2008, 09:33 PM
#5
Junior Member
Re: VB6 Wait for condition
Do
DOEVENTS
Loop Until Condition = True
Is the correct way to do it.
-
Mar 19th, 2008, 10:08 PM
#6
Re: VB6 Wait for condition
I interpreted the question as to force app to wait; do nothing or ignore everything, until condition is met
DigiRev has a good idea but I believe the api CreateWaitEvent may not apply here, not sure. You may want to look over that last api.
Another option is to set a timer and check for the flag every timer event; uses little cpu time depending on timer interval
Another option is to create a message pump, abort all messages, until the condition flag is set. If interested I can show you an example
However, if I misunderstood & what is wanted is an alternative cpu-friendly DoEvents, then....
I am not a big fan of a Do : DoEvents : Loop because cpu is generally maxed out while loop is running. Also DoEvents allows re-entrance into your form, allowing one to click buttons, menus, close the form and other stuff. Since this can have adverse effects on the app, it should be considered and coded against if desired.
Edited: Here is a modified DoEvents loop, using the WaitMessage API. I also included a link to a nice conversation on its use, pros & cons. Recommend giving a quick 2 minute read. Also note that any DoEvents call allows re-entrance.
Code:
' declarations section
Private Declare Function WaitMessage Lib "user32" () As Long
' the modified doevents loop
Do While Not bCondition ' < bCondition is your condition flag, module-level variable
WaitMessage ' any message: mousemove, click, keybd, timer, just about anything
DoEvents
Loop
Here's the link with the conversation about it
Note: One thing I see that can trip you up with the above is that the app will keep waiting even after your condition is met, until a message comes in. An example would be if the app was hidden while waiting for the condition because hidden windows get little to no messages. If that becomes an issue, recommend forcing a message, something like Me.Caption = Me.Caption or if hidden, simply Me.Show
Last edited by LaVolpe; Mar 19th, 2008 at 10:56 PM.
-
Mar 20th, 2008, 05:08 AM
#7
Thread Starter
Fanatic Member
Re: VB6 Wait for condition
I want in Form_Load() to make a loop that will wait for one process to show up (wait for an application to be opened).
I want my loop to check all the processes and compare to one process (if a process exists exit loop), and when its started loop breaks(breaks the loop and makes an event).
This uses much of CPU, 50% in my case. I used DO LOOP. I also used WAIT in the loop but i want my app to monitor processes all the time, with no delay between searches.
-
Mar 20th, 2008, 06:19 AM
#8
Thread Starter
Fanatic Member
Re: VB6 Wait for condition
Originally Posted by DigiRev
I guess he's wanting an alternative to:
Code:
Do
DoSomething in the meantime
Loop Until Condition = True
Which seems to use as much CPU is available at the time. I'd be interested in hearing of alternative methods. I think some people use an API timer and sometimes a regular timer which is less CPU intensive.
For launching a process I think there is WaitForObject (or whatever it is) but I'm quickly getting into areas I don't know anything about...
Yeah i want to do somethink like that
For example:
Code:
Do
If a process exists then
MsgBox("It exists")
End Do
End if
Loop
-
Mar 20th, 2008, 10:08 AM
#9
Re: VB6 Wait for condition
Use Sleep together with DoEvents, it won't take 100% of the CPU, and also it does not lock the application, like this:
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Pause(ByVal PauseTime As Single)
Dim EndTime As Single
EndTime = Timer + PauseTime
Do Until Timer >= EndTime
Sleep 10
DoEvents
Loop
End Sub
-
Mar 20th, 2008, 10:10 AM
#10
Thread Starter
Fanatic Member
Re: VB6 Wait for condition
Originally Posted by CVMichael
Use Sleep together with DoEvents, it won't take 100% of the CPU, and also it does not lock the application, like this:
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub Pause(ByVal PauseTime As Single)
Dim EndTime As Single
EndTime = Timer + PauseTime
Do Until Timer >= EndTime
Sleep 10
DoEvents
Loop
End Sub
Thanks!!!
What does DoEvents do?
-
Mar 20th, 2008, 10:13 AM
#11
Re: VB6 Wait for condition
Originally Posted by Dungeon Keeper
Thanks!!!
What does DoEvents do?
Instead of copying & pasting the msdn explanation; here is the link:
http://support.microsoft.com/kb/q158175/
-
Mar 20th, 2008, 10:16 AM
#12
Thread Starter
Fanatic Member
Re: VB6 Wait for condition
Originally Posted by LaVolpe
Thank you. Sorry for lot of my questions. Im new in programming so i dont know much
Edit: I have red that from Microsoft's page but i still dont understand DoEvents
Ive made an example:
I was thinking that nothing will happen unless i do something after clicking on my button but message box appears after clicking on button.
Code:
Private Sub Command1_Click()
DoEvents
MsgBox ("ALO")
End Sub
Last edited by Dungeon Keeper; Mar 20th, 2008 at 10:24 AM.
-
Mar 20th, 2008, 10:27 AM
#13
Thread Starter
Fanatic Member
Re: VB6 Wait for condition
Code:
Private Sub Command1_Click()
Do While DoEvents
Loop
MsgBox ("ALO")
End Sub
when i execute this code, i click on my button 10 times and nothing happens but when i close my app, msgbox appears 10 times, why?
And on execution of this code my CPU is 50% used
Code:
Private Sub Command1_Click()
X = Timer()
Do While X + 20 > Timer()
DoEvents
Loop
MsgBox (X)
End Sub
Last edited by Dungeon Keeper; Mar 20th, 2008 at 10:37 AM.
-
Mar 20th, 2008, 10:33 AM
#14
Re: VB6 Wait for condition
You are using the loop incorrectly, not: Do While DoEvents.
You will continue looping forever or until app closes unless you put in the loop a condition to exit. Generally, one adds a module-level (i.e., declarations section) boolean value to their form, sets it to false before entering the loop, then elsewhere in the code it is set to True (when a condition is met) to exit the loop. Look at some of the above examples previously posted to your question.
-
Mar 20th, 2008, 10:43 AM
#15
Thread Starter
Fanatic Member
Re: VB6 Wait for condition
Last edited by Dungeon Keeper; Mar 20th, 2008 at 10:48 AM.
-
Mar 20th, 2008, 01:54 PM
#16
Re: VB6 Wait for condition
Originally Posted by Dungeon Keeper
This one is 'Resolved' i guess
If this is, indeed, the case, then you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.
Thank you.
-
Mar 20th, 2008, 04:01 PM
#17
Thread Starter
Fanatic Member
Re: VB6 Wait for condition
Originally Posted by Hack
If this is, indeed, the case, then you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.
Thank you.
Im new here, i didnt know i can make that. I thought only moderators and admins are allowed to do that.
Thank you for letting me know this
I did it now
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
|