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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:33:42+00:00 2026-06-16T16:33:42+00:00

I have this code: public async Task AsyncMethod() { await Task.Factory.StartNew(() => { throw

  • 0

I have this code:

    public async Task AsyncMethod()
    {
        await Task.Factory.StartNew(() =>
        {
            throw new Exception();
        });
    }

    public ActionResult Index()
    {
        var t1 = Task.Factory.StartNew(() => { throw new Exception(); });
        var t2 = Task.Factory.StartNew(() => { throw new Exception();});
        var t3 = Task.Factory.StartNew(async () => { await AsyncMethod(); });

        try 
        {
            Task.WaitAll(t1, t2, t3);
        }
        catch (AggregateException ex)
        {
            var count1 = ex.InnerExceptions.Count;
            var count2 = ex.Flatten().InnerExceptions.Count;

            throw;
        }

        return View();
    }

I would like to understand why the count1 and count2 variables are 2 and not 3 and how can I get the third exception inside AsyncMethod?

  • 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-16T16:33:44+00:00Added an answer on June 16, 2026 at 4:33 pm

    I would like to understand why the count1 and count2 variables are 2 and not 3 and how can I get the third exception inside AsyncMethod?

    Task.Factory.StartNew returns a basic Task. If you pass it an async delegate, then the returned Task only represents the beginning of the async method (up until the point it yields to its caller).

    You should be using Task.Run with async code. Task.Run will create a Task wrapper for an async delegate, so the Task returned from Task.Run represents the entire async method.

    Stephen Toub has an excellent blog post detailing the differences between Task.Run and Task.Factory.StartNew.

    Also, as usr mentioned, you have problems with deadlocks whenever you block on Task rather than await it in a GUI or ASP.NET context. I have a blog post that goes into detail about this deadlock problem. You should use await Task.WhenAll instead of Task.WaitAll.

    So, here’s your code with both changes applied:

    public async Task AsyncMethod()
    {
        await Task.Run(() =>
        {
            throw new Exception();
        });
    }
    
    public async Task<ActionResult> Index()
    {
        var t1 = Task.Run(() => { throw new Exception(); });
        var t2 = Task.Run(() => { throw new Exception();});
        var t3 = Task.Run(async () => { await AsyncMethod(); });
    
        try 
        {
            await Task.WhenAll(t1, t2, t3);
        }
        catch (Exception)
        {
            var ex1 = t1.Exception.InnerException;
            var ex2 = t2.Exception.InnerException;
            var ex3 = t3.Exception.InnerException;
    
            throw;
        }
    
        return View();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my ViewModel I have this code: Logs = new ObservableCollection<Log>(); Logs = Task.Factory.StartNew(()
I have this sample code for async operations (copied from the interwebs) public class
I have this code public static Boolean freq[] = new Boolean[Global.iParameter[2]]; freq[Global.iParameter[2]] = false;
For Andriod I have this code: public Tank(int color) { bounds = new RectF();
I have a Portable Class Library (PCL) method like this: public async Task<string> GetLineStatuses()
I have this code: public class Window extends JFrame { public Window(){ ... JButton
i have this code public interface Interfcae1 { void OP1(); } public interface Interfcae2
I have this code: public class Area { Texture2D point; Rectangle rect; SpriteBatch _sB;
I have this code (which is way simplified from the real code): public interface
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>

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.