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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:41:29+00:00 2026-06-05T03:41:29+00:00

Starting from an issue I had on my code, I’ve created this simple app

  • 0

Starting from an issue I had on my code, I’ve created this simple app to recreate the problem:

private async void button1_Click(object sender, EventArgs e)
{
    Task task = Task.Run(() =>
    {
        TestWork();
    });
    try
    {
        await task;
        MessageBox.Show("Exception uncaught!");
    }
    catch (Exception) { MessageBox.Show("Exception caught!"); }
}

private async void button2_Click(object sender, EventArgs e)
{
    Task task = TestWork();
    try
    {
        await task;
        MessageBox.Show("Exception uncaught!");
    }
    catch (Exception) { MessageBox.Show("Exception caught!"); }
}

private async Task TestWork()
{
    throw new Exception();
}

The code for button1_Click will not catch the exception. I’ve verified that this is because I’m not awaiting the TestWork async method. Indeed I’ve a warning message from Visual Studio that inform me I’m not awaiting the method. However the solution compile and I’m scared this can happen somewhere else in my code if I use extensively the async/await. So can you please explain the reason and peraphs give some golden rule to avoid it?

P.S.: It works if in the code for button1_Click I write:

Task task = Task.Run(async () =>
{
    await TestWork();
});
  • 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-05T03:41:34+00:00Added an answer on June 5, 2026 at 3:41 am

    In your first button1_Click implementation, you are ignoring the result of the Task returned by TestWork.

    In your modified version, the await checks for an Exception and propogates it for you to catch.

    In fact, the compiler warning you are seeing is normally important. If TestWork actually ran something on a different thread, then because the wrapper Task in your first implementation doesn’t wait for TestWork to complete, it would just complete itself as soon as TestWork had started.

    Writing it this way makes it clearer:

    Task task = Task.Run( () =>
        {
            Task t = TestWork();
            // ignore t
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an issue with my app when starting an SimpleBaseGameActivity from another SimpleBaseGameActivity
Starting from Android 3.2 I have this strange problem. It's very easy to reproduce:
Starting from this official statement that all xyAndroid versions previous API 11 doesn't support
Edit: I fixed the problem by just starting from scratch. Sorry to waste y'alls
I'm having a weird issue - in my code I am fetching data from
On a HTML canvas I have multiple points starting from 1 to N, this
Starting from scratch with very little knowledge of .NET, how much ASP.NET should I
Starting from scratch is hard. How do you do it? I quite like color
Starting from Visual Studio 2010, iterating over a set seems to return an iterator
starting from a mostly fresh CE 1.6.2.0 installation, I want to remove the default

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.