Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vb.net - How do I "DoEvents" in WPF?

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

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

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?

Edit: Possibly this?

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

             End Sub))
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

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.

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))

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)))

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

  • Are more than one statement
  • Do not return a value (meaning it's just a single call to a method, not a function)

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#.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...