Doevents?-VBForums
Page 1 of 2 12 LastLast
Results 1 to 40 of 49

Thread: Doevents?

  1. #1

    Thread Starter
    Hyperactive Member notquitehere188's Avatar
    Join Date
    Dec 2004
    Posts
    403

    Doevents?

    What does doevents do
    somebody told me that it slowed down loops but i dont understand what they mean
    i would assume it is to stop do loops from hoggin the computer but where should i put it and what does it accomplish in the end
    It's not just Good, It's Good enough!



    Spelling Eludes Me

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Doevents?

    If have a large loop it's a good idea to use DoEvents as it frees up your CPU to do run other processes that are also open.

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Doevents?

    Don't put it in every iteration of the loop. Use if x mod 100 = 0 then doevents, or something like that. with my progressbar in the form load, without doevents, it never showed the form or the progressbar. Just one doevents fixed it though.

    Using it will prevent the program from hanging while your loop is running. If noting else can go on, then there isn't much point. Try it with no doevents and see if anything hangs. Add one or more to allow user intervention.

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    if you have a long loop, do it evertime, but i dont suggest 100, maybe 10 tops

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    This is a code or RobDog888....

    VB Code:
    1. Option Explicit
    2. 'Add a reference to Microsoft HTML Object Library
    3. 'Add component Microsoft Internet Controls
    4.  
    5. 'Add a command button (Command1)
    6. 'Add a web browser control to the form (WebBrowser1)
    7. Dim hDoc As MSHTML.HTMLDocument
    8. Dim hCol As MSHTML.IHTMLElementCollection
    9. Dim hInp As MSHTML.HTMLInputElement
    10. Dim hSub As MSHTML.HTMLInputButtonElement
    11. Dim hTxt As MSHTML.HTMLInputTextElement
    12.  
    13. Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
    14.  
    15. Private Sub Command1_Click()
    16.     Dim iStart As Integer
    17.     Dim iEnd As Integer
    18.     iStart = 0
    19.     iEnd = 0
    20.     If WebBrowser1.LocationURL <> "http://www.upcdatabase.com/nocheckdigit.pl" Then
    21.         WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl"
    22.     End If
    23.     Do While WebBrowser1.Busy = True
    24.         DoEvents
    25.         Sleep 500
    26.     Loop
    27.     Set hDoc = WebBrowser1.Document
    28.     Set hInp = hDoc.getElementById("upc")
    29.     hInp.focus
    30.     hInp.Value = "07800008846"
    31.     SendKeys "{ENTER}", True
    32.     Do While WebBrowser1.Busy = True
    33.         [B]DoEvents[/B]
    34.         Sleep 500
    35.     Loop
    36.     Set hInp = Nothing
    37.     'Parse the elements and read the values of the data returned
    38.     'Set document variable = to the new results documents page
    39.     Set hDoc = WebBrowser1.Document
    40.     'Find the description:
    41.     iStart = InStr(1, hDoc.body.innerHTML, "<TD>Description</TD>") + 37
    42.     iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>")
    43.     MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
    44.     'Find the Size/Weight:
    45.     iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Size/Weight</TD>") + 37
    46.     iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>")
    47.     MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
    48.     'Find Manufacturer:
    49.     iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Manufacturer</TD>") + 38
    50.     iEnd = InStr(iStart, hDoc.body.innerHTML, "(<A href=")
    51.     MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
    52.     'Find the Entered/Modified:
    53.     iStart = InStr(iStart, hDoc.body.innerHTML, "<TD>Entered/Modified</TD>") + 42
    54.     iEnd = InStr(iStart, hDoc.body.innerHTML, "</TD></TR>")
    55.     MsgBox Mid$(hDoc.body.innerHTML, iStart, iEnd - iStart)
    56. End Sub
    57.  
    58. Private Sub Form_Load()
    59.     WebBrowser1.Navigate2 "http://www.upcdatabase.com/nocheckdigit.pl"
    60. End Sub
    61.  
    62. Private Sub Form_Resize()
    63.     If Me.WindowState <> vbMinimized Then
    64.         WebBrowser1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight - 450 'Make adjustment for command button
    65.     End If
    66. End Sub
    67.  
    68. Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, URL As Variant)
    69. '    Set hDoc = WebBrowser1.Document
    70. End Sub

    Do you think the highlighted DoEvents is not necessary there?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    yes, but i dont agree with the sleep 500, i dont like that at all

  7. #7
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    What do you mean by "YES"? I guess there are really situations wherein DoEvents is necessary right?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Doevents?

    Why not check if there are any Events to "do". Call the GetQueueStatus API, if there are any events waiting to be processed call DoEvents.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Const QS_HOTKEY = &H80
    4. Private Const QS_KEY = &H1
    5. Private Const QS_MOUSEBUTTON = &H4
    6. Private Const QS_MOUSEMOVE = &H2
    7. Private Const QS_PAINT = &H20
    8. Private Const QS_POSTMESSAGE = &H8
    9. Private Const QS_SENDMESSAGE = &H40
    10. Private Const QS_TIMER = &H10
    11. Private Const QS_ALLINPUT = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)
    12.  
    13. Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
    14.  
    15. Private Sub Form_Click()
    16.     Dim lngDoEventCnt As Long
    17.     Dim lngLoopCount As Long
    18.    
    19.     Do
    20.         If GetQueueStatus(QS_ALLINPUT) <> 0 Then
    21.             lngDoEventCnt = lngDoEventCnt + 1
    22.             DoEvents
    23.         End If
    24.        
    25.         lngLoopCount = lngLoopCount + 1
    26.     Loop Until lngLoopCount = 2000000
    27.    
    28.     Debug.Print lngDoEventCnt
    29. End Sub

    On my pc, clicking the Form and not doing anything results in 38 DoEvents being called. Much better than 200,000.

  9. #9
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    yes of course doevents is neccesary. BruceVD, thanks alot for that code-its real interesting

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    Hi brucevde, are those the only inputs or there are others that you didnt list?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Doevents?

    The code covers all possible message types except for one called QS_RAWINPUT, which is only available on WinXP. Here is MSDN's explanation of raw input

    There are many user-input devices beside the traditional keyboard and mouse. For example, user input can come from a joystick, a touch screen, a microphone, or other devices that allow great flexibility in user input. These devices are collectively known as Human Input Devices (HID). The raw input API provides a stable and robust way for applications to accept raw input from any HID, including the keyboard and mouse.

  12. #12
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    In the code of RobDog888 as I have posted, I think DoEvents was needed there so it could wait for WebBrowser1_NavigateComplete2 to occur before proceeding, what kind of input is that?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Doevents?

    They have changed the site a bit. The url to use has changed.

    VB Code:
    1. WebBrowser1.Navigate2 "http://www.upcdatabase.com/item.pl"

    And the UPC's have changed also. You can use a can of coke.

    VB Code:
    1. Private Sub Form_Load()
    2.   Text1.Text = "4963406"
    3. End Sub

  14. #14
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    I have just posted RobDog888's code as an example that uses DoEvents, and I wish to know what kind of input could be derived in such situations....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  15. #15
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    can you explain what you mean? doevents doesnt take any input. Here, an example will work best

    VB Code:
    1. For i = 0 to 100000
    2. x = x + 2
    3. next i

    VB Code:
    1. For i = 0 to 100000
    2. x = x + 2
    3. doevents
    4. next i

    VB Code:
    1. For i = 0 to 100000
    2. If (i mod 10) = 0 then doevents
    3. doevents
    4. next i
    see how the first one freezing the system until its done, while the doevents ones let you continue without crashing any application

  16. #16
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    This is the input I am referring to: QS_ALLINPUT, its been used by brucevde in his code....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  17. #17
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    im still not sure of your question, but ALLINPUT refers to anything that the computer needs to process(mouse move, refresh of a picture..etc)

  18. #18
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    This ff. code is supposed to wait WebBrowser1_NavigateComplete2 before proceeding to the next lines, what kind of input is it or what kind of input will occur so doevents will be fired?

    VB Code:
    1. Do While WebBrowser1.Busy = True
    2.     If GetQueueStatus(QS_ALLINPUT) <> 0 Then
    3.         DoEvents
    4.     End If
    5. Loop
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  19. #19
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    if the mouse moves or gets clicked, something needs to be drawn on the screen, a key is pressed,a timer is fired, or a message is sent from one program to another. It will only perform a doevents when the computer has something waiting in the quene.

  20. #20
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    Hmmmmnnnn... I guess DoEvents is unnecessary in the code I cited above if I will do nothing and just wait till it finish processing? I need not click the mouse or press any keys while its doing its stuff so it means no need for DoEvents? Could it still fire WebBrowser1_NavigateComplete2 without DoEvents?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  21. #21
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    No, depending on how long it takes the page to load you need doevents. If for some reason the page didnt load, youre program would freeze and your computer would lock up. Its essential in long loops to use it, depending on your connection you should use it.

    Think of it this way, your entire computer will lock up while the browser is busy.

  22. #22
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    I was thinking that DoEvents was necessary so that the CPU could also process the WebBrowser1_NavigateComplete2..... Did I think wrong?

    I know the idea behind DoEvents, I just want to be clarified in the situation I'm raising....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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

    Re: Doevents?

    dee-u: why are you having the wait loop there? Couldn't you have the code that is after the DoEvents loop to be in the NavigateComplete sub?

  24. #24
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    Because the processing is done asynchronously....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  25. #25
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Re: Doevents?

    Quote Originally Posted by |2eM!x
    yes, but i dont agree with the sleep 500, i dont like that at all
    i concur
    keep that there or your app will be at 100% cpu usage until the page is loaded

  26. #26
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Doevents?

    Hmm. I don't like the idea of DoEvents because we're operating in a preemptive multitasking operating system. I suspect that there's no way that VB can use 100% of CPU resources even though performance monitor, and task manager show this to be the case. For instance, even though 100% is shown, I have no performance problems with Word, and screen updating doesn't suffer either . . .

    However, I understand the need to ensure that application appears to working cooperatively with the operating system, so I here's my idea of where to put DoEvents:

    On a form with a progressbar set for 0..100

    VB Code:
    1. Option Explicit
    2.  
    3. Private WithEvents mLongOperation As CLongOperation
    4.  
    5. Private Sub Command1_Click()
    6.  
    7.     Command1.Enabled = False
    8.    
    9.     Set mLongOperation = New CLongOperation
    10.     mLongOperation.DoSomething
    11.     Set mLongOperation = Nothing
    12.    
    13.     Command1.Enabled = True
    14.    
    15. End Sub
    16.  
    17. Private Sub mLongOperation_ProgressNotification(ByVal Value As Long)
    18.     ProgressBar1.Value = Value
    19.     DoEvents
    20. End Sub

    . . .and the class declared on the form:

    VB Code:
    1. Option Explicit
    2.  
    3. Public Event ProgressNotification(ByVal Value As Long)
    4.  
    5. Private Sub Class_Initialize()
    6. '
    7. End Sub
    8.  
    9. Private Sub Class_Terminate()
    10. '
    11. End Sub
    12.  
    13. Public Function DoSomething()
    14.  
    15.     On Error GoTo ERR_DoSomething
    16.    
    17.     Dim i As Long
    18.     Dim MaxValue As Long
    19.    
    20.     MaxValue = 100000000
    21.    
    22.     For i = 0 To MaxValue
    23.         If i Mod 1000 = 0 Then
    24.             RaiseEvent ProgressNotification((i / MaxValue) * 100)
    25.         End If
    26.     Next
    27.    
    28.     Exit Function
    29.    
    30. ERR_DoSomething:
    31.     Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
    32. End Function

    If I were doing this across the network I would use a COM Callback. ie I only use VB events for local stuff. I use the COM marshaller for everything else.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  27. #27
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    Quote Originally Posted by VaxoP
    i concur
    keep that there or your app will be at 100% cpu usage until the page is loaded
    A program of mine used this and the CPU Usage was very high....

    VB Code:
    1. Do While WebBrowser1.Busy = True
    2.     If GetQueueStatus(QS_ALLINPUT) <> 0 Then
    3.         DoEvents
    4.     End If
    5. Loop

    But when I inserted an Sleep in it the CPU Usage went down very low!

    VB Code:
    1. Do While WebBrowser1.Busy = True
    2.     Sleep 1
    3.     If GetQueueStatus(QS_ALLINPUT) <> 0 Then
    4.         DoEvents
    5.     End If
    6. Loop

    Could anybody tell me what happen? It seems its the opposite of what VaxoP said!
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  28. #28
    Frenzied Member
    Join Date
    Jul 2003
    Posts
    1,269

    Re: Doevents?

    Quote Originally Posted by dee-u
    A program of mine used this and the CPU Usage was very high....

    VB Code:
    1. Do While WebBrowser1.Busy = True
    2.     If GetQueueStatus(QS_ALLINPUT) <> 0 Then
    3.         DoEvents
    4.     End If
    5. Loop

    But when I inserted an Sleep in it the CPU Usage went down very low!

    VB Code:
    1. Do While WebBrowser1.Busy = True
    2.     Sleep 1
    3.     If GetQueueStatus(QS_ALLINPUT) <> 0 Then
    4.         DoEvents
    5.     End If
    6. Loop

    Could anybody tell me what happen? It seems its the opposite of what VaxoP said!
    Originally Posted by |2eM!x
    yes, but i dont agree with the sleep 500, i dont like that at all


    i concur
    keep that there or your app will be at 100% cpu usage until the page is loaded
    -------------------


    i mean, keep the sleep statement there or you will have 100% cpu usage

  29. #29
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    That's what I quoted and in my case it was exactly the opposite that I achieved, with the Sleep in there the CPU Usage actually went down....
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  30. #30
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    is that because it freezes your computer? maybe it makes everything stop working, thus the lower percentage

  31. #31
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    I think it was really CPU intensive, refreshing a page constantly but when I inserted Sleep 1 in it the CPU Usage went really low and the app still works as expected... Why is that?
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  32. #32
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    im not sure anymore..i honestly have no clue...I guess we will have to wait for one of the elite members here to help with this one

  33. #33
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Doevents?

    The worst thng about DoEvents is it can screw with your code execution. If you have events firing that depend on data handled by other events, placing a Doevents in one event can cause the other event to start executing. This sort of thing really messes up program flow.

    This is because DoEvents allows *all* other code that is waiting to be exeucted, to run. Sleep on the other hand doesn't allow any code from your process to run, it merely pauses the process for the specified time and this allows other process to get the CPU control.

    In a loop, Sleep is the best thing to allow other processes to run, however if you have things waiting in your own process then DoEvents is really the only way to go. (there are other APIs you can use but I've forgotten them ) Brucevde's code looks like a really way of using it

  34. #34
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,941

    Re: Doevents?

    As Per Penagate's statement, it seems my code below is valid?

    VB Code:
    1. Do While WebBrowser1.Busy = True
    2.     Sleep 1
    3.     If GetQueueStatus(QS_ALLINPUT) <> 0 Then
    4.         DoEvents
    5.     End If
    6. Loop
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  35. #35
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Doevents?

    Yes. You can further speed that up by trimming the fat from your If block a bit.
    VB Code:
    1. Do While WebBrowser1.Busy = True
    2.     Sleep 1
    3.     If (GetQueueStatus(QS_ALLINPUT)) Then _
    4.         DoEvents
    5. Loop
    I believe that compiles to smaller code, unless the VB optimiser is more advanced than I thought.

  36. #36
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Doevents?

    I didn't think that the WebBrowser could lock up your program, if a DoEvents wasn't used. Shouldn't it automatically have a type of DoEvents? Or be its own thread or whatever?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  37. #37
    Lurker
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: Doevents?

    No it doesn't lock it up it's just that dee-u is using a loop and repeatedly checking the .Busy property.

  38. #38
    Super Moderator Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Doevents?

    DoEvents will yield the execution of the current running code and give the application time to process other events that might be queued such as repainting the window and process keyboard and mouse events.

    In the code posted by Dee-u above there is a loop that allows the webbrowser control to finish loading the page before it continues. Calling DoEvents here would be necassary for the window to refresh itself. The user could for example have switch to another window and then switched back to this form. The user would then expect that the window to be redrawn. Or he might hit the maximize button and of course then expect it to maximize, none of which it would do until the sub has finished unless there was a DoEvents call.

    The reason RobDog added a Sleep call is simply to let the application wait half a second before it continues the loop. If it would have looked like this:
    VB Code:
    1. Do While WebBrowser1.Busy
    2.     DoEvents
    3. Loop
    DoEvents would probably have been called a million times if it takes a while for the webbrowser to load the page, calling it that many times would be a complete waste of time. This code:
    VB Code:
    1. Do While WebBrowser1.Busy
    2.     If GetQueueStatus(QS_ALLINPUT)
    3.         DoEvents
    4.     End If
    5. Loop
    wouldn't call DoEvents more then necassary but on the other hand it would call the GetQueueStatus API function a million times instead, which is also a complete waste of CPU cycles. The user wouldn't probably care if the code takes one half of a second extra to complete after the page is loaded (in real life it would be more like 250 ms extra).

    Merri suggested that the code after the loop should be moved to the NavigateComplete event instead and therefor the loop would be unnecassary in the first place, and that is correct. However doing the HTML parsing in that event suggests that you always want to parse the HTML document after you have navigated to a particular URL, which might not be the case so I think the code is just fine as it looks. Of course if you really want to use the GetQueueStatus API the code should probably look something like this:
    VB Code:
    1. Do While WebBrowser1.Busy
    2.     If GetQueueStatus(QS_ALLINPUT)
    3.         DoEvents
    4.     End If
    5.     Sleep 500
    6. Loop
    I'm not saying that sleeping for 500 ms is the ideal time, you could change that to 100 or 200 or whatever you find would be appropriate but sleeping here and then process mouse, keyboard, and paint events is a good practice for that example.
    Joacim Andersson
    If anyone's answer has helped you, please show your appreciation by rating that answer.
    I'd rather run ScriptBrix...
    Joacim's view on stuff.

    MVP

  39. #39
    Super Moderator RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,486

    Re: Doevents?

    I cant believe I read a whole thread on DoEvents.

    Interesting to see all the points of view on my loop. I sure am glad I got the loop correct or you all would have been
    bashing me for 40+ posts
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Star Wars Gangsta Rap Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Corsair H100i v2 water cooler, Geforce GTX1060, Samsung M.2 500 GB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2010, VS 2010

  40. #40
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Doevents?

    i was just about to get to that : )

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.