|
-
Mar 7th, 2007, 04:51 AM
#1
Thread Starter
Lively Member
DoEvents. what is it useful for
DoEvents. what is it useful for.
does it just give control back to the user ie enable controls
during some processing?
-
Mar 7th, 2007, 04:56 AM
#2
Re: DoEvents. what is it useful for
DoEvents will force the application to process any pending events. If you're performing a long operation and an event is raised it must wait until you finish your operation to be handled. By calling DoEvents you pause what you're doing and handle any events in the queue. Many of those events will be raised by the user's actions, like clicking a button. That's why you were under that misconception.
You should generally avoid calling DoEvents. If a situation seems like it requires it then you should think about using multithreading. Only in the most trivial cases should actually use DoEvents to keep a UI responsive during long operations.
-
Mar 7th, 2007, 06:16 AM
#3
Re: DoEvents. what is it useful for
Or use SendKeys.Flush
to wait for the application to process keystrokes and other operating system messages that are in the message queue. This is equivalent to calling Application.DoEvents until there are no more keys to process.
Also note, that VB60's DoEvents sleeps the thread to yield, while .NET's Application.DoEvents does not.
I prefer SendKeys.Flush which does not seem to crash, as Application.DoEvents did in some scenarios.
My SendInputToWindow class, would not work the same without SendKeys.Flush, and System.Threading.Thread.Sleep.
In fact the combined method SleepFlush is the key element, which generally mimics the old DoEvents, without being as buggy.
-
Mar 7th, 2007, 06:59 AM
#4
Frenzied Member
Re: DoEvents. what is it useful for
 Originally Posted by jmcilhinney
DoEvents will force the application to process any pending events. If you're performing a long operation and an event is raised it must wait until you finish your operation to be handled. By calling DoEvents you pause what you're doing and handle any events in the queue. Many of those events will be raised by the user's actions, like clicking a button. That's why you were under that misconception.
You should generally avoid calling DoEvents. If a situation seems like it requires it then you should think about using multithreading. Only in the most trivial cases should actually use DoEvents to keep a UI responsive during long operations.
The most common situation where I use DoEvents is where I have a progress bar that is incremented on each iteration of a loop. If DoEvents is not used in each iteration, the progress bar does not increment. Are you saying that there is a better way to achieve this without using DoEvents?
-
Mar 7th, 2007, 02:31 PM
#5
New Member
Re: DoEvents. what is it useful for
You could refresh the form each time. That might work.
I am either at home or at school. Do you have a problem with that? Because of you do, I will run you down in my car. And if you really tick me off, I will boil you alive and devour your entrails.  Watch yourself. 
-
Mar 7th, 2007, 05:50 PM
#6
Re: DoEvents. what is it useful for
You could refresh the form each time. That might work.
Yes, .Refresh() would be the way to go, since DoEvents does that AND a bunch of other extra things that you dont need to do.
-
Mar 7th, 2007, 06:37 PM
#7
Re: DoEvents. what is it useful for
You shouldn't even refresh the form, just the progress bar.
-
Mar 7th, 2007, 06:48 PM
#8
Frenzied Member
Re: DoEvents. what is it useful for
When I was learning VB6, I read that DoEvents could and should be used liberally to make screens and controls appear sharply. Therefore, I have often used it routinely in my Form_Activate(d) event to ensure that controls are drawn quickly to provide a better visual experience.
Are you saying that you don't agree that DoEvents should be used liberally in this way or that the nature of DoEvents has changed in .NET such that it should no longer be used in such a way?
Is the refresh method of the progress bar better than using DoEvents?
-
Mar 7th, 2007, 07:19 PM
#9
Re: DoEvents. what is it useful for
Most things happen so fast in a modern app that calling DoEvents offers little to no benefit. If you do have a longer-running operation then calling DoEvents can help in certain situations. An example is if the user drags another form over yours. The problem is that if it's not needed then you're calling DoEvents repeatedly for no reason, thus making your code less efficient. As I said, if what you're doing makes a significant difference to the responsiveness of the UI then you might want to consider using multi-threading. The new BackgroundWorker makes this virtual child's play. With virtually all new processors being multi-core, now is the time start thinking seriously about how multi-threading can be used to make all apps more efficient.
-
Mar 8th, 2007, 06:14 AM
#10
Re: DoEvents. what is it useful for
Are my posts not getting through?
I thought I said:
Yes, .Refresh() would be the way to go,...
jmcilhinney,
Is this not what you mean?
ProgressBar1.Refresh()
robertx,
or that the nature of DoEvents has changed in .NET
Yes, that's close enough.
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
|