Beruflich Dokumente
Kultur Dokumente
Remember Me?
New Posts FAQ Calendar Forum Actions Quick Links Advanced Search
VBForums Visual Basic Visual Basic 6 and Earlier [VB6] - about Doevents
If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing
messages, select the forum that you want to visit from the selection below.
Results 1 to 35 of 35
joaquim
[VB6] - about Doevents
Thread Starter
PowerPoster can anyone explain to me why we need use the doevents?
and if we can avoid it for speed?
Click H
Join Date: Apr 2007
Posts: 3,358
SamOscarBrown
Re: [VB6] - about Doevents
PowerPoster
Sure:
In many situations, the DoEvents keyword can be a real boon. As you may know, this keyword yields operation to the
operating system so that it can process other events. Many times after initiating a procedure, you may want to pause
execution to gather further information from an end user via a form. For example, consider a project with two forms.
Join Date: Aug 2012 The main form performs a calculation and displays the results. The second form lets an end user manually enter two
Location: NC, USA numbers for the calculation. When you click the second form's Submit button, the main form multiplies the user-
Posts: 7,427 entered numbers and displays the results.
Typically, to handle this feature, you might use DoEvents to wait until the user has entered the two numbers.
SamOscarBrown
Re: [VB6] - about Doevents
PowerPoster
Also, jmsrickland responded to a post just recently with these words of wisdom:
Usually you will see DoEvents in a loop. It allows the system to take care of other things while you are in the loop. If
you don't put a DoEvent in a loop, especially a lengthy loop nothing else can take place until the loop ends. What do
you think will happen if you get stuck in a loop and your loop does not have a DoEvents in it? You can't get out of the
Join Date: Aug 2012 loop unless you kill the program. So by putting a DoEvent in that loop you could add a button that will allow you to
Location: NC, USA either break out of the loop or examine what is taking place. Without the DoEvent the button or any other control is
Posts: 7,427 useless until the loop ends.
DataMiser
Re: [VB6] - about Doevents
PowerPoster
Basically DoEvents should be used sparingly and you may run into issues depending on how your app is coded.
Join Date: Feb 2012
Location: West Virginia If you are running a long loop like perhaps reading 10,000 records from a database or something like that without the
Posts: 13,260 doevents then form repaints and such will not happen plus the program will not respond to windows messages and the
UI will appear to be frozen. You may also get a not responding message on the title bar or in task manager even
though the program is still working fine. If you switch to another program and back during such a process you may not
see your program at all until either it hits a doevents or completes the loop.
On the downside if you have a timer running or a Winsock or Com control when a doevents is hit the code may jump to
the sub for one of those events and start executing even though that is not what you may want to happen. So again
they must be used wisely. Often you can over come potential display issues by using a call to refresh rather than
doevents without the other side effects.
Reply With Quote
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster thanks to all.
but i have 1 problem on 1 code
Code:
Private Sub GetPic()
Join Date: Apr 2007
Posts: 3,358 Picture2.Picture = Nothing
UserControl.Picture = Nothing
UserControl.BorderStyle = 0
UserControl.MaskPicture = Nothing
API_DoEvents
UserControl.Picture = Picture2.Image
UserControl.MaskPicture = UserControl.Image
UserControl.MaskColor = UserControl.BackColor
UserControl.BorderStyle = 1
End Sub
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Lo
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Lon
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Lo
For x = 0 To SrcWidth - 1
For y = 0 To SrcHeight - 1
'geting the source and destination colors\pixels
can anyone advice me for don't use the doevents, in these case?
Bonnie West
Re: [VB6] - about Doevents
Default Member
Perhaps the UserControl's Refresh method might help.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency:
Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo
0
My CodeBank Contributions
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
Reply With Quote
techgnome
Re: [VB6] - about Doevents
PowerPoster
DoEvents doesn't slow down code (not directly) ... what it does is tells Windows, hey, if you have any pending
messages, go ahead and process them... things like updating the UI, processing clicks, etc.
That's usually why people use doevents, so they can see their updates on the screen. UI threads are among the lowest
priority when it comes to processing which is why you're not seeing updates, the system is busy with other things.
Join Date: May 2002 That said, you should NOT call doevents on each and every single iteration of the loop... you should only call it on
Posts: 32,904 occasion.
Code:
For a as integer = 1 to 10000
if a mod 100 = 0 then
doevents
end if
next
This will call DoEvents every 100th iteration through the loop.
-tg
* I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
* I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
* How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
* How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my
data, where'd it run off to??? *
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster
Originally Posted by Bonnie West
Perhaps the UserControl's Refresh method might help.
Join Date: Apr 2007
Posts: 3,358 i have tested now and don't works
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster
Originally Posted by techgnome
DoEvents doesn't slow down code (not directly) ... what it does is tells Windows, hey, if you have any pending messages,
go ahead and process them... things like updating the UI, processing clicks, etc.
That's usually why people use doevents, so they can see their updates on the screen. UI threads are among the lowest
Join Date:
Posts:
Apr 2007
3,358 priority when it comes to processing which is why you're not seeing updates, the system is busy with other things.
That said, you should NOT call doevents on each and every single iteration of the loop... you should only call it on
occasion.
Code:
For a as integer = 1 to 10000
if a mod 100 = 0 then
doevents
end if
next
This will call DoEvents every 100th iteration through the loop.
-tg
i understand what you mean, but can i avoid the doevents?
my transparentalphablend() use getpixel() and setpixelV() like you have seen the code(module). but the sample image
is with my avatar size. but i can see 1 second(or less) for change the image... so how can i avoid the doevents?
VB6 2D Sprite control
Bonnie West
Re: [VB6] - about Doevents
Default Member
If you avoid DoEvents, then your UI will freeze. If you call it often, then your code will slow down. techgnome just
showed you a compromise between the two.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency:
Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo
0
My CodeBank Contributions
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
dw85745
Re: [VB6] - about Doevents
PowerPoster
Appleman wrote an article years ago (Visual Basic Programmers Journal as I recall) that describes what's known as a
Join Date: Jul 2001 "State Machine". It is slightly different approach to event programming and is designed to try and eliminate UI lag.
Location: Tucson, AZ Alternatively, if can wrap up what your trying to do in an ActiveX control, then it "effectivwely" will run in a separate
Posts: 2,166 thread (like a menu) and will have no lag.
If you search for "State Machine" this forum I believe I've posted this article or a reference to it in the past.
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster
Originally Posted by Bonnie West
If you avoid DoEvents, then your UI will freeze. If you call it often, then your code will slow down. techgnome just showed
you a compromise between the two.
Join Date: Apr 2007
Posts: 3,358
i understand what you mean, but, without speaking in my sub(i can use DIB's instead), how can i speed up my code?
dw85745 i'm trying search what you said... thanks
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster
Originally Posted by Bonnie West
If you avoid DoEvents, then your UI will freeze. If you call it often, then your code will slow down. techgnome just showed
you a compromise between the two.
Join Date: Apr 2007
Posts: 3,358
i understand what you mean, but, without speaking in my sub(i can use DIB's instead), how can i speed up my code?
dw85745 i'm trying search what you said... thanks
VB6 2D Sprite control
dw85745
Re: [VB6] - about Doevents
PowerPoster
An internet search will lead you to:
Join Date: Jul 2001
Location: Tucson, AZ http://www.desaware.com/tech/statemachineintro.aspx
Posts: 2,166
firoz.raj
Re: [VB6] - about Doevents
Frenzied Member
Also, jmsrickland responded to a post just recently with these words of wisdom:
Join Date: Jan 2009
Usually you will see DoEvents in a loop. It allows the system to take care of other things while you are in the loop. If you
don't put a DoEvent in a loop, especially a lengthy loop nothing else can take place until the loop ends. What do you think
Location: Watch
Window(Shift+f9)
Posts: 1,879 will happen if you get stuck in a loop and your loop does not have a DoEvents in it? You can't get out of the loop unless
you kill the program. So by putting a DoEvent in that loop you could add a button that will allow you to either break out of
the loop or examine what is taking place. Without the DoEvent the button or any other control is useless until the loop
ends.
So by putting a DoEvent in that loop you could add a button that will allow you to either break out of the loop or examine
what is taking place. Without the DoEvent the button or any other control is useless until the loop ends.
when i press btbreak in between finish look .it does not break .so why you say it will allow to either break out of the
loop ? . let me know some idea .
Code:
Option Explicit
Private Sub btbreak_Click()
DoEvents
End Sub
Bonnie West
Re: [VB6] - about Doevents
Default Member
firoz.raj
Re: [VB6] - about Doevents
Frenzied Member
No it does not break out of the loop when press btbreak. let me know please
Join Date: Jan 2009
Code:
Location: Watch
Window(Shift+f9) Option Explicit
Posts: 1,879
Dim break As Boolean
Private Sub btbreak_Click()
break = True
End Sub
Bonnie West
Re: [VB6] - about Doevents
Default Member
I've just tested your code, Print x is too fast. Try something more taxing. Also, do it in the Form_Activate event so that
the Form is actually shown.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency:
Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo
0
My CodeBank Contributions
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
firoz.raj
Re: [VB6] - about Doevents
Frenzied Member
it still the same it still does not braek while press button.
Join Date: Jan 2009
Code:
Location: Watch
Window(Shift+f9) Option Explicit
Posts: 1,879
Dim x As Long
Dim break As Boolean
Private Sub btbreak_Click()
break = True
End Sub
Private Sub Form_Activate()
For x = 1 To 1000000000
Print x
DoEvents
If break Then Exit For
Next
End Sub
Bonnie West
Re: [VB6] - about Doevents
Default Member Put a Beep statement after the Next keyword. If you hear it when the Form shows up, then the loop has just finished.
You never got the chance to click the button. Replace the Print x line with something more CPU-intensive.
On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency:
Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo
0
My CodeBank Contributions
Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)
firoz.raj
Re: [VB6] - about Doevents
Frenzied Member
i tried another way round it still seems to be same .any more idea to test doevents keyword
Join Date: Jan 2009
Code:
Location: Watch
Window(Shift+f9) Option Explicit
Dim x As Long
Posts: 1,879
Dim break As Boolean
Private Sub btbreak_Click()
break = True
End Sub
DataMiser
Re: [VB6] - about Doevents
PowerPoster
Your code works but the way you \have written it you will not be able to tell for sure that it worked.
Join Date: Feb 2012
Location: West Virginia try this
Posts: 13,260
Code:
Option Explicit
Dim x As Long
The longer you wait to press button 2 the higher the number will be as it completed more cycles before exit
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster sorry why you use loops, if in these case it's diferent?
i understand that doevents see if theres any depend event and execute it
but why my code don't works without doevents?
because i'm simply clean the usercontrol and picturebox.
Join Date: Apr 2007
Posts: 3,358
jmsrickland
Re: [VB6] - about Doevents
PowerPoster
Sure it breaks out of the loop. Your code works. You just didn't know it did.
Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it
is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.
DataMiser
Re: [VB6] - about Doevents
PowerPoster
techgnome
Re: [VB6] - about Doevents
PowerPoster
Actually, doevents WILL slow things down...because it's releasing processing time to the REST of the system... UI
updates, other apps, other processes, etc... that's the reason why you shouldn't call DoEvents on every single iteration
in a loop... but to do it on a controlled basis, like I explained in post #7... if you want your code to run fast, dump the
DoEvents... if you want the UI to be responsive, then you need the DoEvents... you can't have both in their purest
forms though... you'll need to find the right combination of how often it gets called and performance, which will only
Join Date: May 2002 come with experimentation.
Posts: 32,904
-tg
* I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
* I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
* How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
* How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my
data, where'd it run off to??? *
DataMiser
Re: [VB6] - about Doevents
PowerPoster
Actually do events can slow things down, but in the case mentioned there is no loop, there is only one call to do events
Join Date: Feb 2012 so 99.9% likely that it would be completely unnoticed in terms of program speed. Only if there happens to be some
Location: West Virginia pending event that takes a noticable amount of time at the point where the do events is called would there be any
Posts: 13,260
detectable slow down.
Edit: Looks like the piece of code I am referring to has been removed, but there was just some graphics calls in a
routine with a single do events an no loop so that is why I say it should nto slow things down, but the OP also said that
his code did not work without the doevents so I have no idea why this would be or how he was able to tell that it was
slower with it since it did not work without it. Sounds like the info is a bit less than accurate.
Edit2: Opps I was mistaken abotu it being removed I was looking at the wrong post for it. The OP posted code where
there is a call to API_DoEvents in the first block of code post #5 and it is not in a loop. I assume this is where he is
talking about.
Ben321
Re: [VB6] - about Doevents
Fanatic Member
Also, DoEvents is ABSOLUTELY NECESSARY in computer games. The game's loop needs to keep running to keep
Join Date: Oct 2008 updating the NPCs (nonplayable characters) to make them run around on the screen or whatever they do, but it needs
Posts: 1,013 to yield control to other events (such as your keyboard key presses) so you can can control the player's character.
DataMiser
Re: [VB6] - about Doevents
PowerPoster
Join Date: Feb 2012 Also, DoEvents is ABSOLUTELY NECESSARY in computer games
Location: West Virginia
Posts: 13,260
That is just not true, a timer can and should be used rather than a busy loop with doevents.
Ben321
Re: [VB6] - about Doevents
Fanatic Member
The ultimate result is that using a timer configured to any speed faster than 100ms per tick will be unreliable and
produce jerky motion.
Better technique is to use a Do Loop which really ties up the CPU, such that it won't get "distracted" doing "background
tasks". Then to sync the graphics to the display's built in screen refresh speed (30fps) you put the line Sleep 33 (which
is an API call set to delay the game's computing the next frame for the duration of one screen refresh period) just
before the line that says Loop.
Then in any part of the game's main loop that needs input, add a DoEvents, followed by a series of GetKeyState
(another API call) lines to get the keys needed at that point in the game. This way the CPU is only allowed to be
bothered with accepting input (events) at very specific points in the cycle of the Loop.
With timer though it is as if it is ALWAYS in a DoEvents state, ALWAYS willing to let the CPU be distracted by other
inputs, making the game's frame timing very unreliable. Do NOT USE TIMER with computer games, unless you want
the game to have very crappy graphics.
DataMiser
Re: [VB6] - about Doevents
PowerPoster
I totally disagree. A busy loop is not the way to go.
Join Date: Feb 2012
Location: West Virginia Then again VB6 is the wrong tool for game programming that requires fuild graphics.
Posts: 13,260
btw I am well aware of the limitations of the timer control
firoz.raj
Re: [VB6] - about Doevents
Frenzied Member
i did not see any difference between the following shots of code .
Join Date: Jan 2009 i have been using 64 bit microprocessor .
Location: Watch
Window(Shift+f9)
Posts: 1,879
Code:
For a as integer = 1 to 10000
if a mod 100 = 0 then
form1.print a
doevents
end if
next
This
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster i'm sorry, i build these thread and i need some advices
thinking on these:
1 - clear the usercontrol;
2 - by some reason the usercontrol isn't clean, then i must use the 'doevents';
Join Date: Apr 2007 3 - copy the container image(include controls) with GetDC() and Bitblt() API functions;
Posts: 3,358 4 - combine the 2 images for make the Opacity and show the result in usercontrol.
why, in these case, i need the 'doevents' for clean the usercontrol?
joaquim
Re: [VB6] - about Doevents
Thread Starter
PowerPoster
Originally Posted by DataMiser
I totally disagree. A busy loop is not the way to go.
Join Date: Apr 2007 Then again VB6 is the wrong tool for game programming that requires fuild graphics.
Posts: 3,358
btw I am well aware of the limitations of the timer control
"Then again VB6 is the wrong tool for game programming that requires fuild graphics."
depends if you use directx ou API graphics functions
joaquim
Re: [VB6] - about Doevents
Thread Starter heres the sample project: http://depositfiles.com/files/5ijt993id
PowerPoster please someone test it and tell something.
thanks
VBForums Visual Basic Visual Basic 6 and Earlier [VB6] - about Doevents
Posting Permissions
You may not post new threads BB code is On
You may not post replies Smilies are On
You may not post attachments [IMG] code is On
You may not edit your posts [VIDEO] code is On
HTML code is Off
Forum Rules
Terms of Service | About Us | Privacy Notice | Contact Us | Advertise | California - Do Not Sell My Info
Copyright 2020 TechnologyAdvice. All Rights Reserved.
Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact
how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products
available in the marketplace.
All times are GMT -5. The time now is 10:59 AM.