Steven Liekens
Steven Liekens

Reputation: 14133

Non-blocking Task.wait in .NET 4?

I'm trying to wait for a Task result in .NET 4, much like you can by using the await keyword in .NET 4.5. I just can't figure out how to do it though...

My code (to prove that I'm atleast trying):

    Private Function GetXDocumentFromWebLocationAsync(ByVal request As WebRequest) As XDocument
        Dim queryTask As Task(Of WebResponse)
        queryTask = task(Of WebResponse).Factory.FromAsync(AddressOf request.BeginGetResponse, AddressOf request.EndGetResponse, Nothing, Nothing)
        Return XDocument.Load(queryTask.Result.GetResponseStream)
    End Function

As you would expect, the GetResponse calls are executed on a different thread, but the function has to wait for the result before it can return. Unfortunately this blocks my main thread until the task has completed and I don't have the slightest clue on how to make it wait without blocking. I don't want to use the Async CTP either because that's just running away from the problem.

What's the secret sauce?

Upvotes: 0

Views: 2980

Answers (1)

svick
svick

Reputation: 245038

There is no secret sauce. If you want to make a method that returns with the result of some asynchronous operation, you simply have to block the thread until the operation completes.

But there are other ways to achieve the same thing. The “old” way of doing this in .Net is the same WebRequest uses: have two methods BeginGetXDocument and EndGetXDocument. You can then pass a delegate to the Begin method that will get executed when the operation completes.

Another way of doing this is to return a Task from your method. To do that, you can use ContinueWith():

Private Function GetXDocumentFromWebLocationAsync(ByVal request As WebRequest) As Task(Of XDocument)
    Dim queryTask As Task(Of WebResponse)
    queryTask = Task(Of WebResponse).Factory.FromAsync(
        AddressOf request.BeginGetResponse, AddressOf request.EndGetResponse, Nothing, Nothing)
    Return queryTask.ContinueWith(
        Function(antecendent) XDocument.Load(antecendent.Result.GetResponseStream))
End Function

This way, the consumer of the method can choose to wait for the result synchronously (using Wait() or Result), or it can use ContinueWith() again.

When using ContinueWith() in GUI applications, you have to be careful: the continuation is run on a ThreadPool thread by default. To run your continuation on the GUI thread, you can use TaskScheduler.FromCurrentSynchronizationContext(). Or a method specific for your GUI library (Dispatcher.Invoke() in WPF, Control.Invoke() in Winforms).

Upvotes: 3

Related Questions