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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:45:48+00:00 2026-06-10T09:45:48+00:00

Under what scenarios would one want to use public async Task AsyncMethod(int num) instead

  • 0

Under what scenarios would one want to use

public async Task AsyncMethod(int num)

instead of

public async void AsyncMethod(int num)

The only scenario that I can think of is if you need the task to be able to track its progress.

Additionally, in the following method, are the async and await keywords unnecessary?

public static async void AsyncMethod2(int num)
{
    await Task.Factory.StartNew(() => Thread.Sleep(num));
}
  • 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-10T09:45:50+00:00Added an answer on June 10, 2026 at 9:45 am
    1. Normally, you would want to return a Task. The main exception should be when you need to have a void return type (for events). If there’s no reason to disallow having the caller await your task, why disallow it?

    2. async methods that return void are special in another aspect: they represent top-level async operations, and have additional rules that come into play when your task returns an exception. The easiest way is to show the difference is with an example:

    static async void f()
    {
        await h();
    }
    
    static async Task g()
    {
        await h();
    }
    
    static async Task h()
    {
        throw new NotImplementedException();
    }
    
    private void button1_Click(object sender, EventArgs e)
    {
        f();
    }
    
    private void button2_Click(object sender, EventArgs e)
    {
        g();
    }
    
    private void button3_Click(object sender, EventArgs e)
    {
        GC.Collect();
    }
    

    f‘s exception is always "observed". An exception that leaves a top-level asynchronous method is simply treated like any other unhandled exception. g‘s exception is never observed. When the garbage collector comes to clean up the task, it sees that the task resulted in an exception, and nobody handled the exception. When that happens, the TaskScheduler.UnobservedTaskException handler runs. You should never let this happen. To use your example,

    public static async void AsyncMethod2(int num)
    {
        await Task.Factory.StartNew(() => Thread.Sleep(num));
    }
    

    Yes, use async and await here, they make sure your method still works correctly if an exception is thrown.

    For more information see: https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming

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

Sidebar

Related Questions

Under what scenarios would one want to connect to a host other than localhost?
Android kills my service in the package com.net.myspeechservice under some certain scenarios, for example
I'm building a Flex application that will need run under two different deployment scenarios:
the following scenario: We wanna use one root-folder for our eclipse projects. Since i
Wikipedia provides information about one of the most common scenarios for exploiting a reflected
I have a very basic one table scenario; tblFamiles entryID : Int name: Text
Scenario: A client calls WebService A on the LAN. WebService A is running under
Under Ubuntu 10, I installed using: npm install js2coffee Seems to install smoothly I
Under what specific situation is it ok (or even recommended) to present end-users with
Under Windows 7 I have a batch file for checking the status of some

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.