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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:41:35+00:00 2026-06-18T04:41:35+00:00

I am new to asynchronous programming, so after going through some async sample codes,

  • 0

I am new to asynchronous programming, so after going through some async sample codes, I thought of writing a simple async code

I created a simple Winform application and inside the Form I wrote the following code. But its just not working

private Task<string> methodAsync() {
    Thread.Sleep(10000);
    return "Hello"; //Error: Cannot implicitly convert type 'string' to 'System.Threading.Tasks.Task<string>'
}

private async void button1_Click(object sender, EventArgs e)
{
    string s = await methodAsync();
    MessageBox.Show(s);
}

Could someone please put some light here..

  • 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-18T04:41:36+00:00Added an answer on June 18, 2026 at 4:41 am

    The listed return type of the method is Task<string>. You’re trying to return a string. They are not the same, nor is there an implicit conversion from string to Task<string>, hence the error.

    You’re likely confusing this with an async method in which the return value is automatically wrapped in a Task by the compiler. Currently that method is not an async method. You almost certainly meant to do this:

    private async Task<string> methodAsync() 
    {
        await Task.Delay(10000);
        return "Hello";
    }
    

    There are two key changes. First, the method is marked as async, which means the return type is wrapped in a Task, making the method compile. Next, we don’t want to do a blocking wait. As a general rule, when using the await model always avoid blocking waits when you can. Task.Delay is a task that will be completed after the specified number of milliseconds. By await-ing that task we are effectively performing a non-blocking wait for that time (in actuality the remainder of the method is a continuation of that task).

    If you prefer a 4.0 way of doing it, without using await , you can do this:

    private Task<string> methodAsync() 
    {
        return Task.Delay(10000)
            .ContinueWith(t => "Hello");
    }
    

    The first version will compile down to something that is more or less like this, but it will have some extra boilerplate code in their for supporting error handling and other functionality of await we aren’t leveraging here.

    If your Thread.Sleep(10000) is really meant to just be a placeholder for some long running method, as opposed to just a way of waiting for a while, then you’ll need to ensure that the work is done in another thread, instead of the current context. The easiest way of doing that is through Task.Run:

    private Task<string> methodAsync() 
    {
        return Task.Run(()=>
            {
                SomeLongRunningMethod();
                return "Hello";
            });
    }
    

    Or more likely:

    private Task<string> methodAsync() 
    {
        return Task.Run(()=>
            {
                return SomeLongRunningMethodThatReturnsAString();
            });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently read an article about c#-5 and new & nice asynchronous programming features
I am new to programming and I am confused about asynchronous socket programming. For
I'm new to PHP. I am familiar with ASP.NET which support asynchronous programming. That
I am new to Objective C programming. I have created two threads called add
I'm new to Node.js and asynchronous programming in general, and I have a problem.
Recently I try to learn about the new C# feature, async/await keywords for asynchronous
I have the following code regarding asynchronous sockets programming: public void ServerBeginAceept(ushort ServerPort) {
new user to the site here. I'm working on a simple asynchronous tcp server.
I'm using naga to do some asynchronous socket programming. However I need to be
I am new to Node would appreciate some advice on writing to an array.

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.