我如何“DoEvents"在 WPF 中? [英] How do I "DoEvents" in WPF?

查看:31
本文介绍了我如何“DoEvents"在 WPF 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过 C# 版本如下:

I've read that the C# version is as follows:

Application.Current.Dispatcher.Invoke(
          DispatcherPriority.Background,
          new Action(delegate { }));

但是我无法弄清楚如何将空委托放入 VB.NET,因为 VB.NET 似乎不支持匿名方法.想法?

However I cannot figure out how to put the empty delegate into VB.NET, as VB.NET does not appear to support anonymous methods. Ideas?

可能是这个?

Application.Current.Dispatcher.Invoke(
  DispatcherPriority.Background,
  New Action(Sub()

             End Sub))

推荐答案

VB.NET 确实支持匿名委托,但仅作为单语句函数.(多语句匿名函数和匿名Subs已添加到.NET 4中的VB10)

VB.NET does support anonymous delegates, but only as single-statement functions. (Multi-statement anonymous functions and anonymous Subs have been added to VB10 in .NET 4)

为了提供上下文,DoEvents 旨在允许单线程环境在工作完成时更新 UI 并处理其他 Windows 消息.从另一个线程调用 DoEvents(或者,正如您在这里所做的那样,通过在调度程序上执行null"函数来间接调用)应该没有任何好处,因为 UI 线程应该正在更新自己.

To provide context, DoEvents was designed to allow a single-threaded environment to update the UI and process other Windows messages while work was being done. There should be no benefit to calling DoEvents (or, as you're doing here, doing so indirectly by executing a "null" function on the dispatcher) from another thread, as the UI thread should be updating by itself.

不过,为了回答您的问题,最简单的选择是这样的:

In the interest of answering your question, though, the easiest option would be something like this:

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, _
    New Action(Function() 7))

但是,同样,我看不出这实际上会实现什么.

But, again, I can't see what this would actually accomplish.

如果您只是在寻找如何在 UI 线程上执行代码(如 Windows 窗体中的 Control.Invoke),那么 Dispatcher.Invoke (这是您正在使用的)是正确的,您只需要将您的内联匿名方法转换为谨慎的函数并将其作为委托传递.可能有一些你可以匿名离开.例如,如果您所做的只是更新进度条,您可以这样做:

If what you're looking for is simply how to execute code on the UI thread (like the Control.Invoke from Windows forms), then Dispatcher.Invoke (which is what you're using) is correct, you'll just have to translate your inline anonymous methods into discreet functions and pass those as delegates. There may be some that you can get away with leaving as anonymous. For example, if all you're doing is updating a progress bar, you could do:

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, _
    New Action(Function() progressBar.Value = 100))

这是有效的,因为所有赋值都返回存储在赋值左侧的值.但是,您不能像这样调用子程序(除非 SomeFunctionName 返回值,否则以下内容不会编译):

This works because all assignments return the value that was stored in the left side of the assignment. You could not, however, just call a sub like this (the following won't compile unless SomeFunctionName returns a value):

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, _
    New Action(Function() SomeFunctionName(params)))

换句话说,C# 代码中的任何匿名委托:

In other words, any of the anonymous delegates that are in the C# code that either:

  • 不止一个声明
  • 不要返回值(意味着它只是对方法的一次调用,而不是函数)

然后,您必须为这些函数创建函数并将委托传递给这些函数,而不是像在 C# 中那样内联代码.

Then you'll have to create functions for those and pass delegates to those functions rather than inlining the code as you have in C#.

这篇关于我如何“DoEvents"在 WPF 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆