See the end of hansPassant answer for details. Add a comment. Active Oldest Votes. Peter Mortensen Hans Passant Hans Passant k gold badges silver badges bronze badges. This is only the tip of the ice berg, though. I have been using Application.
DoEvents without problems until I added some UDP features to my code, which resulted in the problem described here. I'd love to know if there's a way around that with DoEvents. Would benefit from a more practical solution than generally "use threads". For example, the BackgroundWorker component manages threads for you, avoiding most of the colorful outcomes of foot-shooting, and it doesn't require bleeding edge C language versions.
DoEvents is OK, provided that you are a conscientious, disciplined, and well educated developer with good judgement. And the problem is that Application. DoEvents is particularly enticing to developers who are lacking at least one of those qualities temporarily at least. It can be, but it's a hack. Direct from the MSDN page that thedev referenced: Calling this method causes the current thread to be suspended while all waiting window messages are processed.
So Microsoft cautions against its use. Community Bot 1 1 1 silver badge. It's worth noting that that post is from , before. NET 2. Also, the Task library in. NET 4. Why would it be a hack if Microsoft has provided a function for the very purpose for which it is often needed? Craig Johnston - updated my answer to more fully detail why I believe DoEvents falls into the category of hackery. That coding horror article referred to it as "DoEvents spackle.
Bill W Bill W 1, 1 1 gold badge 17 17 silver badges 29 29 bronze badges. Nice answer, but if I may suggest a solution for the progressbar thing which is waays better , I would say, do your work in a separate thread, make your progress indicator available in a volatile, interlocked variable, and refresh your progressbar from a timer. This way no maintenance coder is tempted to add heavyweight code to your loop.
DoEvents or an equivalent is unavoidable if you have hefty UI processes and don't want to lock up the UI. First option is to not do large chunks of UI processing, but this can make the code less maintainable. However, await Dispatcher. Yield does a very similar thing to DoEvents and can essentially allow a UI process that locks up the screen to be made for all intents and purposes async.
Frederik Gheysels Frederik Gheysels More detail on my case BeginExecuteNonQuery ; while! Sleep 10 ; Application. TamW TamW 71 1 1 silver badge 2 2 bronze badges. Refresh does not update the window, though. If a user selects another window on the desktop, clicking back to your window will not have any effect and the OS will list your application as unresponsive. DoEvents does a lot more than a refresh, as it interacts with the OS through the messaging system.
Here are some approaches taken from a blog post by Tom Miller : Set your form to have all drawing occur in WmPaint, and do your rendering there.
Before the end of the OnPaint method, make sure you do a this. Invalidate ; This will cause the OnPaint method to be fired again immediately. Doevents actually does something similar, but you can do this without the extra allocations. Name Application. DoEvents Method. Class System. Syntax Application. Description Allows the operating system to process events and messages waiting in the message queue.
Each time the form handles an event, it processes all the code associated with that event. All other events wait in the queue. While your code handles the event, your application does not respond. For example, the window does not repaint if another window is dragged on top. If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox and add DoEvents to your code, your form repaints when another window is dragged over it.
If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing. For more information on messaging, see User Input in Windows Forms. Unlike Visual Basic 6. Sleep method. Most people try to call DoEvents to simply "update an otherwise frozen user interface", or to push through a Paint handler or click handler.
Well, the interface is frozen because you thought it was appropriate to neglect to return from an event handler - end of story. People that choose this approach should try to become comfortable refactoring loops and other flow control constructs to be friendly to the main thread's message pump and dispatcher.
DoEvents , because it gives some advice on some of the basic techniques involved in such refactoring. It's hard to keep track of state nicely when you let tasks run asynchronously, but it really makes for the best quality user interface when it stays responsive, so I highly recommend it.
Queue the lengthy operation as a task which will run in another thread and dispatch updates to the main thread using Control. For event handlers, track state in member variables -- not local variables, or loops. The content you requested has been removed.
Ask a question. Quick access. Search related threads. Remove From My Forums. Asked by:. Archived Forums. Windows Forms General. Sign in to vote.
0コメント