|
-
Jan 7th, 2007, 08:03 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] How to use "doevents" and "Me"
Can you guys explain me "doevents" and "me" ?
A short,simple example would be very helpful.
I'm totally clueless about these 2 "items".
Thanks.
-
Jan 7th, 2007, 08:12 PM
#2
Re: How to use "doevents" and "Me"
DoEvents is a command that lets Windows take care of any other tasks at hand. It can seriously slow down a recursive loop, but it will keep the computer from locking up on your project.
Me is another word for "Current Form". In other words, say I was making a Hangman project, and the main form was called frmHangman. Instead of saying frmHangman.Caption or whatever, I could just say Me.Caption. This applies to any property or function for the form.
-
Jan 7th, 2007, 08:14 PM
#3
Re: How to use "doevents" and "Me"
Me is the Form/Class where your code is being written, it doesn't work with modules because they aren't Classes. Example in a Form:
VB Code:
MsgBox Me.Caption
Unload Me
DoEvents allow Windows events to occur, it's usefull when executing a long process (most times with a Loop) to allow your app to keep reacting to events, else it freezes. It's also used to allow a command to be executed Immediately.
Last edited by jcis; Jan 7th, 2007 at 08:18 PM.
-
Jan 7th, 2007, 08:16 PM
#4
Re: How to use "doevents" and "Me"
From VBOnline
Doevents
Code:
Yields execution so that the operating system can process other events.
Syntax
DoEvents( )
Remarks
The DoEvents function returns an Integer representing the number of open forms in stand-alone versions of Visual Basic, such as Visual Basic, Professional Edition. DoEvents returns zero in all other applications.
DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.
DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file. For long-running processes, yielding the processor is better accomplished by using a Timer or delegating the task to an ActiveX EXE component. In the latter case, the task can continue completely independent of your application, and the operating system takes case of multitasking and time slicing.
Caution Any time you temporarily yield the processor within an event procedure, make sure the procedure is not executed again from a different part of your code before the first call returns; this could cause unpredictable results. In addition, do not use DoEvents if other applications could possibly interact with your procedure in unforeseen ways during the time you have yielded control.
Me
Code:
The Me keyword behaves like an implicitly declared variable. It is automatically available to every procedure in a class module. When a class can have more than one instance, Me provides a way to refer to the specific instance of the class where the code is executing. Using Me is particularly useful for passing information about the currently executing instance of a class to a procedure in another module. For example, suppose you have the following procedure in a module:
Sub ChangeFormColor(FormName As Form)
FormName.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Sub
You can call this procedure and pass the current instance of the Form class as an argument using the following statement:
ChangeFormColor Me
Last edited by Andrew G; Jan 7th, 2007 at 08:20 PM.
-
Jan 7th, 2007, 08:21 PM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] How to use "doevents" and "Me"
Thankyou guys! Now I know how to use them.
Marked as resolved ;-)
-
Jan 7th, 2007, 11:50 PM
#6
Fanatic Member
Re: [RESOLVED] How to use "doevents" and "Me"
I asked this question last month, and I am still reading and trying to understand when to use DoEvents. After reading, re-reading Andrew G, so, I have a text to Excel program, reads text, converts to an excel format. I should add a DoEvents ? The Text file could be 2M.
VB Code:
Do While ts.AtEndOfStream <> True
s = Split(ts.ReadLine, mstrSep) ' Split row by Separator
intRow = intRow + 1
If intRow = 1 Then GoSub Setup1
'
For intA = 0 To UBound(s)
oSheet.Cells(intRow, intA + 1).Value = s(intA)
Next
'
Loop
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open# Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
-
Jan 8th, 2007, 01:59 AM
#7
Re: [RESOLVED] How to use "doevents" and "Me"
Try something like this:
VB Code:
Do While ts.AtEndOfStream <> True
s = Split(ts.ReadLine, mstrSep) ' Split row by Separator
intRow = intRow + 1
If intRow = 1 Then GoSub Setup1
If intRow Mod 100 = 0 then DoEvents
'
For intA = 0 To UBound(s)
oSheet.Cells(intRow, intA + 1).Value = s(intA)
Next
'
Loop
Last edited by randem; Jan 8th, 2007 at 02:03 AM.
-
Jan 8th, 2007, 10:29 AM
#8
Fanatic Member
Re: [RESOLVED] How to use "doevents" and "Me"
What is Mod 100 ?
http://msdn2.microsoft.com/en-us/library/basszbdt.aspx
I am reading/researching as I type. So , Pass control to the OS when the remainder is 0 , or every 100 loops...Okay.
I am guessing that MOD requires less CPU time than other comparisons...
Thanks.... I was in Maui for the first time last year (2006)..Lahaina.
Alpha Micro: Alpha Basic, AS400 V5r2, EDI (Trusted Link/ Inovis.com),Access AS/400 via VB6, Qbasic for data conversions. A mix of Hardware too. ASCII Table , New Number to Words/66 digits , AS/400(v5r2) VB6 Viewer/Ask for code(ODBC) ^ What Is Transferring? , Check your Ports #Perfect Passwords , *Slide Bar Example , Logoff, Restart, Shut-Down PC *Keep Form On Top , Opaque Form ^ Create Objects at Run Time @ Check Key Caps Locks # GetTickCount(System Up Time) * Convert text to Excel & Collected Icons + Resize: Form/Text box ^ PC GateWay via Shell $ Drag & Drop Game ! PopUpMenu *Print File/no Open# Timer on Mult Forms ~ Splash & Mult Forms & Lots of Comments + Random/Timer/Guess * Dec >Hex >Oct >Bin % Get MAC (NIC) < saving to Registry > Wookiee Cookies \ BackUpDisk / World Conection SpeedTest $ Glossary Commonly Used Terms # phonetic list @ Detailed Computer Scan
When posting Code, Use tags.. [CODE] *Your Code* [/CODE]
-
Jan 8th, 2007, 06:34 PM
#9
Re: [RESOLVED] How to use "doevents" and "Me"
Mod is short for Modulus which is a mathematical computation (division).
It basically gives the remainder. So IntRowA Mod 100 simply means that divide intRowA by 100 and return just the remainder.
-
Jan 8th, 2007, 07:27 PM
#10
Re: [RESOLVED] How to use "doevents" and "Me"
I think it's a good idea to use DoEvents only when it's needed, which is why I use the GetQueueStatus() API function.
All it requires is to paste some API declarations/constants in your program and then a simple if/then statement in your loop.
This will make loops execute a lot faster and utilize a lot less CPU.
Here's some code from allapi.net
VB Code:
Private Const QS_HOTKEY = &H80
Private Const QS_KEY = &H1
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_MOUSEMOVE = &H2
Private Const QS_PAINT = &H20
Private Const QS_POSTMESSAGE = &H8
Private Const QS_SENDMESSAGE = &H40
Private Const QS_TIMER = &H10
Private Const QS_ALLPOSTMESSAGE = &H100
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private Const QS_ALLEVENTS = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)
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)
Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
Private Sub Command1_Click()
'start the loop
Do
'check whether there are mouse-button or keyboard messages
'in the message queue. If there are, call DoEvents
If GetQueueStatus(QS_ALLINPUT) Then DoEvents
Loop Until bCancel = True
End Sub
-
Jan 8th, 2007, 08:42 PM
#11
Fanatic Member
Re: [RESOLVED] How to use "doevents" and "Me"
Okay, I am confused again. I thought the main purpose of DoEvents was to return control to the OS ...so the OS can do its job. Nothing to do with processing user inputs. I do understand that a user might want to abort or alter this loop, but the program should be able to handle these inputs.
-
Jan 8th, 2007, 10:44 PM
#12
Re: [RESOLVED] How to use "doevents" and "Me"
Using that method will delay the user inputs making it hard to even get them in the first place.
-
Jan 9th, 2007, 12:43 AM
#13
Fanatic Member
Re: [RESOLVED] How to use "doevents" and "Me"
Randem, now that make sense, because how the OS processes interrupts such as a user's input. " GetQueueStatus" could be used If the program wanted to know if the user generated an interrupt of some kind....I am good. Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|