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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:56:38+00:00 2026-05-30T05:56:38+00:00

Last year, I wrote a web API library with classic synchronous and asynchronous methods.

  • 0

Last year, I wrote a web API library with classic synchronous and asynchronous methods. I’m now trying to add TaskAsync methods using the new C# Async CTP 3.

I wrote this simple code to encapsulate the sync method:

partial class Client : IClient {
    public string Authenticate(string username, string password)
    {
        // long synchronous code here
    }
    public Task<string> AuthenticateTaskAsync(string username, string password) {
        var p = new { username = username, password = password };
        var task = new Task<string>(p1 => this.Authenticate(p.username, p.password), p);
        return task;
    }
}

Then, from my WPF 4 application, I have a async method using it:

public class LoginViewModel {
    private IClient client;

    // called by an ICommand
    private async Task DoLogin(string username, string password) {
        UpdateStatus("Authenticating...", true);
        try {
            var result = await client.AuthenticateTaskAsync(username, password);
            // do stuff with result
            UpdateStatus("Authenticated. Fetching User informations...", true);
        } catch (Exception ex) {
            UpdateStatus("Authentication error.", ex, false);
        }
    }
}

The issue is: my synchronous method never gets called. The debugger goes to result = await client.AuthenticateTaskAsync(username, password);, the debugger continues its work and never comes back. The breakpoint within the sync Authenticate nevers breaks. UpdateStatus never gets called. Quite strange (I though it was a debugger implementation issue).

Then I looked at how WebClient.DownloadStringTaskAsync is implemented. I changed my API client method to this:

partial class Client : IClient {
    public Task<string> AuthenticateTaskAsync(string username, string password) {
        var tcs = new TaskCompletionSource<string>();

        try {
            tcs.TrySetResult(this.Authenticate(username, password));
        } catch (Exception ex) {
            tcs.TrySetException(ex);
        }

        return tcs.Task;
    }
}

And now it works. Can someone explain why the first code doesn’t work?

  • 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-05-30T05:56:40+00:00Added an answer on May 30, 2026 at 5:56 am

    You’re creating the task but never starting it. It’s created “cold” – it would need something to start it before the function provided to the constructor is actually called.

    Either call Task.Start(), or use TaskEx.Run() or Task.Factory.StartNew() instead of calling the Task constructor:

    public Task<string> AuthenticateTaskAsync(string username, string password) {
        return TaskEx.Run(() => this.Authenticate(username, password));
    }
    

    Note that there’s no need for the anonymous type here – just let the compiler capture the parameters.

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

Sidebar

Related Questions

First a little intro: Last year i wrote this http://dragan.yourtree.org/code/canvas-3d-graph/ Now, i want to
I wrote an One-Time-Password (OTP) generator in C# last year. Now I need to
Last year I developed a data access service for our project using Entity Framework
Following up on my last year's question on documentation, I now want to get
The file upload script I wrote early last year for an internal website has
I have been using LINQ to SQL for the last year and have had
Last year I wrote a Language Service for Visual Studio which added syntax highlighting
Starting to look at my last year project now, and so I'm doing the
I designed a small website last year in visual web developer. I added a
I'm revisiting my class tracking (dirty logic), which I wrote last year. Currently I

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.