Reputation: 33880
VB6 had a DoEvents() method that you called to return control to the OS and mimic multi-threaded behavior in that single threaded environment.
What is the .NET framework equivalent of VB 6 DoEvents()?
Upvotes: 16
Views: 13991
Reputation: 7712
You shouldn't use Application.DoEvents(). It will have reentrancy issues. You should call inside the loop:
await System.Windows.Threading.Dispatcher.Yield()
It will do the same thing. You will need to put it in a async method though. This has the added advantage of marking the method as asynchronous for calling methods. It's nice to suffix methods that call await Dispatcher.Yield with Async(). The method is not naturally async (i.e. it does have CPU bound work) but for all intents and purposes the method becomes async as it will not lock up the calling thread.
Upvotes: 0
Reputation: 5548
The following is a general DoEvents type method
using System;
using System.Windows.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
namespace Utilites
{
/// <summary>
/// Emulates the VB6 DoEvents to refresh a window during long running events
/// </summary>
public class ScreenEvents
{
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void DoEvents()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrame), frame);
Dispatcher.PushFrame(frame);
}
public static object ExitFrame(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
}
}
}
It doesn't need to know about the application.
Upvotes: 5
Reputation: 94
If you call Application.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.0, the DoEvents method does not call the Thread.Sleep method.
Upvotes: 1
Reputation: 263893
you can use Application.DoEvents()
. Why not use Threading class or simply Background Workers? If you are doing in .net environment, don't use DoEvents
. Leave it on VB6.
Upvotes: 25