6

I am trying to call a method that started on a background thread on the UI thread calling BeginInvoke and passing in a delegate as follows:

Dispatcher.BeginInvoke(Function() UpdateApplicationDataUI()) 

to call this method:

Private Sub UpdateApplicationDataUI()
...
End Sub

However, I get an error in the call to BeginInvoke (the UpdateApplicationDataUI portion of the delegate is stating "Expression does not produce a value"). I'm sure I'm missing something simple...any ideas?

Did more research and answered my own question:

Me.Dispatcher.BeginInvoke(Function() New Action(AddressOf UpdateApplicationDataUI))
Random Dev
  • 51,810
  • 9
  • 92
  • 119
Matthew Pitts
  • 809
  • 1
  • 8
  • 13

1 Answers1

16

I think the syntax in VB.net for this is

Dispatcher.BeginInvoke(Sub() UpdateApplicationDataUi())
Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • 1
    Note: If you are working in WPF and you are trying to make an event handler UI responsive, the answer above is only half your solution. First, you need to take the body of your event handler and fork it into it's own (non UI) thread using this: System.Threading.Tasks.Task.Factory.StartNew(Sub() ProcessButtonClickAsync()) Then inside that procedure use the Dispatcher.BeginInvoke to process any UI actions you need (such as status updates). Put "await" in front of the DispatchBeginInvoke to wait on the UI. – Jeff Apr 25 '22 at 18:18