Results 1 to 6 of 6

Thread: DoEvents

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    230

    DoEvents

    I guys I need your help again on DoEvents. As You all know, doevents will repaint the windows so users wil not be annoy when we have a long loop.
    Here's the situation: I load records from access and write it to a text file for exmaple:
    VB Code:
    1. Public Sub LoadName()
    2. Dim rc As Recordset, sql As String
    3. sql = "SELECT * FROM name"
    4. Set rc = cn.Execute(sql)
    5. Do While Not rc.EOF
    6.     Writetext(rc("fistname"))
    7.     DoEvents
    8.     rc.MoveNext
    9. Loop

    PS: i have like 30 000 records.

    As you see, the doevents will repaint the window on every record. I read the forum someone post an alternative way for a doevent for like 10 times by

    VB Code:
    1. For i = 0 To 900
    2. i mod 10 = 0 then DoEvents
    3. Next

    But that only for a for ..next loop. Anybody know a way for the do ..while loop

  2. #2
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: DoEvents

    Simply have a variable that'll do lngA = lngA + 1 and check for the value of lngA. Math is very lightweight, especially in a compiled application.

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

    Re: DoEvents

    Or, you could use DoEvents only when it's needed by using the GetInputState() API function.

    VB Code:
    1. Private Declare Function GetInputState Lib "user32" Alias "GetInputState" () As Long
    2.  
    3. Public Sub LoadName()
    4. Dim rc As Recordset, sql As String
    5. sql = "SELECT * FROM name"
    6. Set rc = cn.Execute(sql)
    7. Do While Not rc.EOF
    8.     Writetext(rc("fistname"))
    9.     [COLOR=Red]If GetInputState Then DoEvents[/COLOR]
    10.     rc.MoveNext
    11. Loop

    There is a better API to use but I forgot what it is. This will speed up your loop a lot and free up some unneeded CPU.

  4. #4
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: DoEvents

    However, making an API call is far more costy than some little math You can combine these though (GetQueueState with some validation might be a better choice though).

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

    Re: DoEvents

    Quote Originally Posted by Merri
    However, making an API call is far more costy than some little math You can combine these though (GetQueueState with some validation might be a better choice though).
    Yup, combining both would probably be even better.

  6. #6
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: DoEvents

    in a do while loop you can still bump a number:

    I.e.

    VB Code:
    1. i = i + 1

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