none
How do you use Dispatcher.InvokeAsync ? RRS feed

  • Question

  • I am failing to find any examples, or to otherwise figure out how to use Dispatcher.InvokeAsync.  This is basically what I'm trying to accomplish, below.  Any help is appreciated.

        Public Async Function GSRGSFunc(ByVal sA As String, ByVal sB as String) As System.Threading.Tasks.Task(Of GSRGS)
            Dim result As GSRGS
            If OnDataThread() Then
                result = Await GSRGSFuncWorker(sA, sB)
            Else
                result = Await GetDataDispatcher.InvokeAsync(Of System.Threading.Tasks.Task(Of GSRGS))(?????????)
            End If
            Return result
        End Function

        Public Async Function GSRGSFuncWorker(ByVal sA As String, ByVal sB as String) As System.Threading.Tasks.Task(Of GSRGS)
            (do awaited stuff)
        End Function

    Monday, February 20, 2017 5:38 PM

All replies

  • Hi Kevin8264,

    Based on your description and related to code. it seems that you Executes the specified Func(Of TResult) asynchronously on the thread the Dispatcher is associated with.

    Please modify your code like this:

    Dim result As GSRGS
             If OnDataThread() Then
                 result = Await GSRGSFuncWorker(sA, sB)
             Else
    
                   Func<GSRGS> methodCall = delegate() { return relatedmethod; };
    
                 result = Await GetDataDispatcher.InvokeAsync(Of GSRGS)(methodCall )
             End If

    For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/hh199385%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    https://msdn.microsoft.com/en-us/library/bb534960(v=vs.110).aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=vb#code-snippet-3

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 21, 2017 8:53 AM
    Moderator
  • Here's the problem with the documentation you referred to; if we ignore that it's in C#, I attempted to write it like this:

                    Dim method As System.Func(Of GSRGS) = _
                        Function()
                            Return GSRGSFuncWorker(sA, sB)
                        End Function

    This errors out because:

    error BC30311: Value of type 'System.Threading.Tasks.Task(Of GSRGS)' cannot be converted to 'GSRGS'.

    Now, I tried:

    result = Await GetDataDispatcher().Invoke(New GSRGSFuncWorkerDelegate(AddressOf GSRGSFuncWorker), sA, sB)

    However... the statement above is executed synchronously, i.e. the thread is stuck until the Invoke finishes.

    The only way I managed to make it actually await was the following (and it doesn't quite seem right):

    Await Task.Run(Async Sub() result = Await GetDataDispatcher().Invoke(New GSRGSFuncWorkerDelegate(AddressOf GSRGSFuncWorker), sA, sB))

    Tuesday, February 21, 2017 2:54 PM
  • Hi Kevin8264,

    >>error BC30311: Value of type 'System.Threading.Tasks.Task(Of GSRGS)' cannot be converted to 'GSRGS'.

    Because you return a asynchronous method. please create a synchronization method which return a GSRGS object.

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, February 22, 2017 8:59 AM
    Moderator
  • I tried:

    result = Await GetDataDispatcher().InvokeAsync(Of GSRGS)( _
        Function()
            Dim t As System.Threading.Tasks.Task(Of GSRGS)
            t = GSRGSFuncWorker(sA, sB)
            t.Wait()
            Return t.Result
        End Function)
    Return result

    However, this caused the code to lock up on the t.Wait() line.  Then I tried:

    result = Await GetDataDispatcher().Invoke(Of System.Threading.Tasks.Task(Of GSRGS))( _
        Function()
            Return GSRGSFuncWorker(sA, sB)
        End Function)
    Return result

    This seems to work.  Now I'm not sure what InvokeAsync is even supposed to do.  But I guess Await Invoke is the correct thing to do.

    Wednesday, February 22, 2017 10:07 PM