tags:

views:

344

answers:

4

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

Use the Function keyword:

Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, New Action(Function() Do ... End Function))
Rob Fonseca-Ensor
The problem is the action does nothing, so what can I put in there that is "nothing"? Also a function returns a variable.
SLC
Does it compile if you just delete the dots? What version of VB / .NET are we talking about here?
Rob Fonseca-Ensor
+1  A: 

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

Adam Robinson
The UI can only update if it gets chance to though, which is at the end of a function. This is why your app becomes unresponsive if you put a loop into it, and even with a thread sleep will not update UI components in a loop without doing an Invoke in windows forms to force a switch to the other thread. I speculate this is the same.
SLC
You can change Function() to Sub() and just have an empty Sub() as I speculated in my edit, because I just confirmed it in code and it seems to work.
SLC
@SLC: This is only true if what you're doing is *taking place on the UI thread*. If the code is executing on another thread, then this is not necessary. **If what you're doing *is* executing on the UI thread and it's taking long enough to require an explicit UI update, then it should be moved to another thread.**
Adam Robinson
@SLC: The `Sub` option only works in VB10 (VisualStudio 2010/.NET 4).
Adam Robinson
A: 

You've got two questions here, so i'm going to add two answers. Here, i'm answering "how to do DoEvents in WPF". Bea Stollnitz covers this on her blog, and here's the code in VB:

publicShared Sub WaitForPriority(priority As DispatcherPriority)
    Dim frame As New DispatcherFrame()
    Dim dispatcherOperation As DispatcherOperation = Dispatcher.CurrentDispatcher.BeginInvoke(priority, New DispatcherOperationCallback(ExitFrameOperation), frame)
    Dispatcher.PushFrame(frame)
    If dispatcherOperation.Status <> DispatcherOperationStatus.Completed Then
        dispatcherOperation.Abort()
    End If
End Sub

Private Shared Function ExitFrameOperation(obj As Object) As Object
    (DirectCast(obj, DispatcherFrame)).[Continue] = False
    Return Nothing
End Function
Rob Fonseca-Ensor
+3  A: 

You could simply call this method if you want something to be refreshed on the UI:

System.Windows.Forms.Application.DoEvents

We are using it on our WPF Window and XBAP applications.

Jojo Sardez