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?
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
WebRequestuses: have two methodsBeginGetXDocumentandEndGetXDocument. You can then pass a delegate to theBeginmethod that will get executed when the operation completes.Another way of doing this is to return a
Taskfrom your method. To do that, you can useContinueWith():This way, the consumer of the method can choose to wait for the result synchronously (using
Wait()orResult), or it can useContinueWith()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 useTaskScheduler.FromCurrentSynchronizationContext(). Or a method specific for your GUI library (Dispatcher.Invoke()in WPF,Control.Invoke()in Winforms).