Results 1 to 17 of 17

Thread: [RESOLVED] VB6 Wait for condition

  1. #1

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Resolved [RESOLVED] VB6 Wait for condition

    How can i make my VB6 app to wait for a condition but not to use much of CPU?

  2. #2
    Addicted Member
    Join Date
    Feb 2008
    Location
    California
    Posts
    151

    Re: VB6 Wait for condition

    System.Threading.Thread.Sleep(miliseconds)

  3. #3
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 Wait for condition

    Quote 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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    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...

  5. #5
    Junior Member
    Join Date
    Feb 2008
    Posts
    31

    Re: VB6 Wait for condition

    Do
    DOEVENTS
    Loop Until Condition = True

    Is the correct way to do it.

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    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.

  8. #8

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Post Re: VB6 Wait for condition

    Quote 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

  9. #9
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,802

    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

  10. #10

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: VB6 Wait for condition

    Quote 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?

  11. #11
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: VB6 Wait for condition

    Quote 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/
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  12. #12

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: VB6 Wait for condition

    Quote Originally Posted by LaVolpe
    Instead of copying & pasting the msdn explanation; here is the link:
    http://support.microsoft.com/kb/q158175/
    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.

  13. #13

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    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.

  14. #14
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  15. #15

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: VB6 Wait for condition

    Quote Originally Posted by LaVolpe
    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.
    Yeah ive seen and tried all examples but my CPU is used too much
    I still didnt get what i was looking for

    Edit: Sorry, one example is working like i wanted, i made some changes and messed something up,
    Thank you guys for help !!!!!

    This one is 'Resolved' i guess
    Last edited by Dungeon Keeper; Mar 20th, 2008 at 10:48 AM.

  16. #16
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB6 Wait for condition

    Quote 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.

  17. #17

    Thread Starter
    Fanatic Member Dungeon Keeper's Avatar
    Join Date
    Mar 2008
    Posts
    590

    Re: VB6 Wait for condition

    Quote 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
  •  



Click Here to Expand Forum to Full Width