AllExperts > Experts 
Search      

Visual Basic

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Visual Basic Answers
Question Library

Ask a question about Visual Basic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Aaron Young
Expertise
I have 11 Years of Programming Experience, 4 years with Visual Basic, I currently work as an Analyst Programmer for Red Wing Business Systems, Inc. I enjoy helping others out when they get stuck and have spent a lot of time in other Forums like VB-World and CodeGuru answering a lot of questions. I have a good knowlegde of VB overall, so will attempt any Question on any part of the Language, from Database Access to ActiveX Control Creation and all the Grey Fuzzy stuff in between. (I do enjoy using the APIs for Subclassing and getting that last drop of functionality out of VB).

 
   

You are here:  Experts > Computing/Technology > Basic > Visual Basic > The DoEvents Function

Topic: Visual Basic



Expert: Aaron Young
Date: 4/14/2005
Subject: The DoEvents Function

Question
What is the DoEvents Function used fore

Answer
Curt,

DoEvents is used to force the application to yield it's use of the processor for one cycle (usually 50ms), it's commonly used to allow other events in your application to fire while you're performing a resource intensive operation.

Here's an example, this code causes the Form's Caption to Count from 1 To 10,000 and also checks to see if you've decided to cancel it by clicking the "Cancel" button.

Run the code with the "DoEvents" line commented out first and you'll notice that you can't click the "Cancel" button to cancel the count.  This is because the loop isn't allowing anything else in your application to use the processor.

Now run the same code again, but this time uncomment the "DoEvents" line, now you'll see that you can cancel the loop at any time without a problem.

'--------------------------------------------------------
Option Explicit

Private m_cancel As Boolean

Private Sub cmdStart_Click()
  Dim i As Long
  m_cancel = False
  For i = 1 To 10000
     Caption = i
     DoEvents
     If m_cancel Then Exit For
  Next
End Sub

Private Sub cmdCancel_Click()
  m_cancel = True
End Sub
'--------------------------------------------------------

Regards,

- Aaron.

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.