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 9177887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:25:46+00:00 2026-06-17T17:25:46+00:00

Edit (after acceptance) It might not be immediately apparent but @Servy’s answer is correct

  • 0

Edit (after acceptance)

It might not be immediately apparent but @Servy’s answer is correct without needing a custom implementation of Unwrap under monodroid – in my comments I said it didn’t exist, but it definitely does.

End edit

I’m writing a bunch of apps that use our RESTful web services and, despite thinking I know how to use tasks properly it turns out I don’t. The scenario is that I have code implemented for Windows Store – using async/await and I need to implement something near-identical for MonoDroid – which doesn’t have that (without some build hacks that I don’t want to use).

I’ve boiled down the problem for this question to a simple set of tasks for getting an integer asynchronously, then, in the continuation, firing another asynchronous task that turns a string built from that integer. In C#5 this would be:

Note – of course I’m using Task.FromResult<T> here in place of actual async code

private async Task<string> GetStringAsync()
{
    return await GetStringFromIntAsync(await GetIntAsync());
}

private async Task<int> GetIntAsync()
{
    return await Task.FromResult(10);
}

private async Task<string> GetStringFromIntAsync(int value)
{
    return await Task.FromResult(value.ToString());
}

To convert this to a continuation-based pattern I tried this:

private Task<string> GetStringAsync()
{
    //error on this line
    return GetIntAsync().ContinueWith(t => GetStringFromIntAsync(t.Result));
}

private Task<int> GetIntAsync()
{
    return Task.FromResult(10);
}

private Task<string> GetStringFromIntAsync(int value)
{
    return Task.FromResult(value.ToString());
}

However, this isn’t correct because the GetStringFromIntAsync method returns a Task<string> meaning that the continuation ends up returning a Task<Task<string>> instead of a Task<string>.

I’ve found that explicitly waiting on the GetStringFromIntAsync method does work, however:

private Task<string> GetStringAsync()
{
    return GetIntAsync().ContinueWith(t =>
    {
        var nested = GetStringFromIntAsync(t.Result);
        nested.Wait();
        return nested.Result;
    });
}

The question is, though – is that the right way to do it – can I not return a continuation of some sort that the caller then waits on?

I have used tasks quite a bit – but not to chain tasks of different types together like this (except with async/await of course) – and feel a bit like I’m going mad here – so any help is greatly appreciated.

  • 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-17T17:25:47+00:00Added an answer on June 17, 2026 at 5:25 pm

    So, first off, your non-async/await imlementations of the second and third implementations is what you should do even when you have async/await. Using it adds nothing except a bit of unneeded overhead.

    As for the first case, no, you don’t want to use Wait. That will block the thread instead of allowing it to be returned to the pool. You simply need to unwrap the task and get it’s inner task. You can use the Unwrap method to do that:

    private Task<string> GetStringAsync()
    {
        return GetIntAsync()
            .ContinueWith(t => GetStringFromIntAsync(t.Result))
            .Unwrap();
    }
    

    Here is an Unwrap function that you can use if one is not available to you.

    public static Task<T> Unwrap<T>(this Task<Task<T>> task)
    {
        var tcs = new TaskCompletionSource<T>();
    
        task.ContinueWith(t =>
        {
            t.Result.ContinueWith(innerT => tcs.SetResult(innerT.Result)
                , TaskContinuationOptions.OnlyOnRanToCompletion);
            t.Result.ContinueWith(innerT => tcs.SetCanceled()
                , TaskContinuationOptions.OnlyOnCanceled);
            t.Result.ContinueWith(innerT => tcs.SetException(innerT.Exception.InnerExceptions)
                , TaskContinuationOptions.OnlyOnRanToCompletion);
        }
            , TaskContinuationOptions.OnlyOnRanToCompletion);
        task.ContinueWith(t => tcs.SetCanceled()
            , TaskContinuationOptions.OnlyOnCanceled);
        task.ContinueWith(t => tcs.SetException(t.Exception.InnerExceptions)
            , TaskContinuationOptions.OnlyOnFaulted);
    
        return tcs.Task;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

oo design basics here... Edit: To clarify after first answer - I'm not asking
FINAL EDIT: After following the answer from Darin Dimitrov, I have found that the
I'm trying to immediately set a row to Edit mode after it is created.
EDIT 1 I apologize but after reading the 2 suggested articles I still don't
EDIT: This post was originally specific to ASP.NET, but after thinking about it I'm
Edit: After reading the responses, I believe the answer is don't do this, hence
Is there a way to convert from System.Windows.Forms.HtmlElement to mshtml.IHTMLElemenet3? --edit after answer accepted
Edit note: After a seemingly enourmous amount of bad feedback MS got from their
Possible Duplicate: Saving changes after table edit in SQL Server Management Studio I need
EDIT: a few weeks after I posted this question Evan Coury wrote an excellent

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.