Application.DoEvents in .NET 3.0 ?
-
Thursday, February 08, 2007 4:01 PM
Hello,
Is there an equivalent of Application.DoEvents in .NET 3.0? If so, what is it?
Thank you
Tom
All Replies
-
Thursday, February 08, 2007 5:20 PM
I don't think there's any such thing in WPF. DoEvents was used basically to process the message queue, but since WPF no longer has the thread affinity of Windows Forms, I'd imagine they've done away with the message queue. -
Thursday, September 27, 2007 9:05 PM
The following line is the moral equivalent of DoEvents in WPF.
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new ThreadStart(delegate { }));
It is probably more dangerous to use..
However it does the trick of pumping the message queue.
Please note that DispatcherPriority.Backround is really important and this code doesn't work if you change the DispatcherPriority.
Christopher
-
Wednesday, August 19, 2009 9:37 AM
This is what I use as a DoEvents
You should use threading where possible, but I found this essential when I want to create visual objects that have not yet been added to the visual tree but needed them to be updated. these off VT objects cannot be in the same thread, so calling the DoEvents from code solves the problem of getting them updated behind the scenes. I have used this when building non pre-viewable documents to be printed.
Regards
Steve
/// <summary>
/// forces crude pause to update visuals - alternative to thread control
/// </summary>
public static void DoEvents()
{
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, new EmptyDelegate(delegate { }));
}
private delegate void EmptyDelegate();- Edited by Steve Thornton Wednesday, August 19, 2009 9:40 AM
-
Tuesday, September 15, 2009 6:53 PMI also wanted to use DoEvents in my WPF application.
I tried following:
System.Windows.Forms.Application.DoEvents();
and surprisingly it is working so far without any issues.
HTH.
AM. -
Wednesday, September 30, 2009 5:43 PMWorking in .NET 2.0, I had to add a reference to c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll, then I used a prefix in the using statement to prevent confusion with Button definition:
using swf=System.Windows.Forms;
...
statusTextBlock.Text = "Exporting...";
swf.Application.DoEvents();
-
Thursday, October 01, 2009 2:50 PM
To clarify a few of these posts:
1) WPF does have thread affinity. The same thread affinity as Windows Forms, actually. So WPF GUI threads have to be STA.
2) WPF does have a windows messaging loop, just like Windows Forms. In fact, WPF windows are Win32 windows, just like Windows Forms.
The way to execute a nested message loop is to use the Dispatcher.PushFrame method. In particular, the MSDN example code for the DispatcherFrame class (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcherframe.aspx) shows how to do a WPF equivalent of System.Windows.Forms.Application.DoEvents.
However, I must state that this is certainly not recommended. Nested message loops are evil, in both Forms and WPF. Try to restructure your code so that it is not necessary, e.g., by using a BackgroundWorker object.
Either way, linking a WPF application with the Windows Forms library and then invoking the Forms' DoEvents is not a great idea.
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
MSBuild user? Try out the DynamicExecute task in the MSBuild Extension Pack source; it's currently in Beta so get your comments in!
Microsoft Certified Professional Developer- Proposed As Answer by Ernesto Perales Soto Thursday, October 01, 2009 9:26 PM
-
Thursday, October 01, 2009 5:37 PM
Either way, linking a WPF application with the Windows Forms library and then invoking the Forms' DoEvents is not a great idea.
In other words:
Doing something evil to accomplish something evil is evil. -
Thursday, October 01, 2009 9:15 PM
Either way, linking a WPF application with the Windows Forms library and then invoking the Forms' DoEvents is not a great idea.
In other words:
Doing something evil to accomplish something evil is evil.
Right. Two wrongs don't make a right.
Evil * evil != good
Evil * evil = evil^2
-Steve
Programming blog: http://nitoprograms.blogspot.com/
Including my TCP/IP .NET Sockets FAQ
MSBuild user? Try out the DynamicExecute task in the MSBuild Extension Pack source; it's currently in Beta so get your comments in!
Microsoft Certified Professional Developer

