Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:25:22+00:00 2026-06-02T23:25:22+00:00

I’m trying to wait for a Task result in .NET 4, much like you

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:25:24+00:00Added an answer on June 2, 2026 at 11:25 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.