C#等效的VB 6 DoEvents

Wat*_* v2 12 .net c# vb.net vb6

VB6有一个DoEvents()方法,您调用该方法将控制权返回给操作系统并模拟该单线程环境中的多线程行为.

VB 6 DoEvents()的.NET框架是什么?

Joh*_*Woo 25

你可以使用Application.DoEvents().为什么不使用线程类或简单的背景工作者?如果您在.net环境中进行操作,请不要使用DoEvents.把它留在VB6上.


Zip*_*pit 8

Application.DoEvents()(WinForms的一部分)

  • @ColeJohnson旧的Application.DoEvents()方法已在WPF中进行了描述,转而使用[*Dispatcher*](http://msdn.microsoft.com/en-us/magazine/cc163328.aspx)或[*后台工作线程*](http://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-to-the-ui/)进行处理你已经描述过了.有关如何使用这两个对象的文章,请参阅链接. (3认同)
  • Application.DoEvents()的存在是为了迁移VB6程序.新开发的程序不应该使用这种方法. (3认同)

Pet*_*ith 5

以下是一般的DoEvents类型方法

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;
    }
}
}
Run Code Online (Sandbox Code Playgroud)

它不需要知道应用程序.