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

  • Home
  • SEARCH
  • 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 9175801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:31+00:00 2026-06-17T16:58:31+00:00

Folks, I’m finding myself in a weird situation. I need to chain tasks of

  • 0

Folks, I’m finding myself in a weird situation. I need to chain tasks of different returning types together. Normally, you can do something like this

Task<T> t = Task<T>.Factory.StartNew(() => ... some T instance);
Task t2 = t.ContinueWith<U>(parent => ...);
return Task.WhenAll(t, t2);

My complication however, lies in using the FromAsync helper method to wrap a Begin/End pair and convert that into a Task. I’m trying to write an asynchronous client using HttpWebRequest and tasks on .NET 4.0 (so await is not an option).

My problem is that the return type of FromAsync is a Task itself which prevents me from using it in a ContinueWith method (ContinueWith expects the return type and wraps the data itself in a Task object).

Here is the code I have so far, which yields the correct functional result, but is not truly asynchronous:

public Task<string> GetHttpRequest(string url, string contentType)
{
    var httpWebRequest = CreateHttpWebRequest(url, "GET", contentType);
    Task<WebResponse> httpTask = Task.Factory.FromAsync<WebResponse>(httpWebRequest.BeginGetResponse, httpWebRequest.EndGetResponse, null);

    return httpTask.ContinueWith(httpAntecedent =>
          {
              WebResponse webResponse = httpAntecedent.Result;
              Stream responseStream = webResponse.GetResponseStream();
              byte[] data = new byte[webResponse.ContentLength];

              var streamReadTask = Task<int>.Factory.FromAsync(responseStream.BeginRead, responseStream.EndRead, data, 0, data.Length, TaskCreationOptions.AttachedToParent);

              return streamReadTask.ContinueWith(parent =>
                  {
                      responseStream.Close();
                      webResponse.Close();

                      return Encoding.UTF8.GetString(data);
                  });
          }).Result;
}
  • 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-17T16:58:32+00:00Added an answer on June 17, 2026 at 4:58 pm

    To rephrase your question, you have Task<Task<string>> and you want to get Task<string> from that, without synchronously waiting for the Task to complete.

    In C# 5.0, you could do this by using double await: return await await task;.

    Without C# 5.0, you can use Unwrap(), it does exactly what you want: return task.Unwrap();.

    If, for some reason, you wanted to do this by yourself, you could use ContinueWith() inside ContinueWith() together with TaskCompletionSource.


    But your code is flawed: it assumes that you will get the whole response in a single read. That’s not guaranteed at all, and in fact won’t work correctly quite often. Doing this properly would require more complicated code and probably also TaskCompletionSource.

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

Sidebar

Related Questions

folks! I need to make such layout: I've got listview and I need to
Folks, I can't find the re-entrant version of syslog() for Linux...is there one? And
folks can you provide me the tutorial link or .pdf for learning basic COM?.
Folks I am developing a HTML5 mobile app for my company which can action
Folks, Sometimes we need to have the Check-out functionality from VSS to be used
folks, How can I differentiate between error codes and status codes in case of
Folks, here is the situation: confirmed that the xlsx file is good on the
Folks, I need a solution that allows drag-and-drop cell over the table. The cells
Folks, we all know that IP blacklisting doesn't work - spammers can come in
folks, How I can get current status for application deployed on websphere (such as

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.