views:

33

answers:

3

Hi all,

This sub works fine:

Private Sub UpdateInfo(ByVal text As String, ByVal timeStamp As DateTime)
    If Me.lstStatus.Dispatcher.Thread Is System.Threading.Thread.CurrentThread Then
        ' Do stuff with 
    Else
        Me.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, New Action(Of String, DateTime)(AddressOf UpdateInfo), text, timeStamp)
    End If
End Sub

But this function doesn't:

Private Function UpdateInfo(ByVal text As String, ByVal timeStamp As DateTime) As ListItem
    If Me.lstStatus.Dispatcher.Thread Is System.Threading.Thread.CurrentThread Then
        Dim l As New ListItem
        ' Do stuff with 
        Return l
    Else
        Me.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Send, New Action(Of String, DateTime)(AddressOf UpdateInfo), text, timeStamp)
        ' Above line doesn't return anything??
    End If
End Function

How do I return my listitem in above function?

Thanks!!!!!

:) Mojo

+1  A: 

Use the return value of BeginInvoke method which is of type DispatcherOperation.

For more info read:

http://msdn.microsoft.com/en-us/library/ms591206.aspx

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatcheroperation.aspx

Grozz
A: 

You should use Func rather than Action. Func has a return value you can define as ListItem

See: http://msdn.microsoft.com/en-us/library/bb549151.aspx

Tor
+2  A: 

Dispatcher.BeginInvoke() is a fire-and-forget method, the delegate target runs at some later time on the UI thread. That's not good enough in your case, you need to wait until the target runs so you can get the return value. Use the Invoke() method instead:

 Return DirectCast(Me.Dispatcher.Invoke(..), ListItem)

And use Func instead of Action. Or AddressOf, the more 'natural' VB.NET way.

Hans Passant
What can I say?? You guys are GREAT! Damn you take away lots of frustrations. Just wanted to let you know, that your help is so much appreciated!!!!! Thanks! :)
MojoDK
@Mojo - You now have enough rep to start voting for posts that are helpful. Don't hesitate to use it!
Hans Passant