28

I know that an async method can only return void or Task. I have read similar methods for Exception handling inside async methods. And I'm new to async programming so I am looking for a straightforward solution.

My async method runs an Sql query. If the query was ok, it should notify the caller with a Boolean true and otherwise a false. My method is currently a void so I have no way of knowing.

private async void RefreshContacts()
{
    Task refresh = Task.Run(() =>
    {
        try
        {
            // run the query
        }
        catch { }
    }
    );
    await refresh;           
}

I simply would like to change async to Task so that in my catch statement the method will return a false and otherwise a true.

5 Answers 5

28

It sounds like you just need to return a Task<bool> then:

private async Task<bool> RefreshContactsAsync()
{
    try
    {
        ...
    }
    catch // TODO: Catch more specific exceptions
    {
        return false;
    }
    ...
    return true;
}

Personally I would not catch the exception, instead letting the caller check the task for a faulted state, but that's a different matter.

5
  • When I change my catch to catch { return false; } after changing my return type to Task<bool> I got this warning message: "Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call." Jul 20, 2015 at 10:00
  • Checking the Task for a faulted state sounds more interesting, can you bring a simple example? Jul 20, 2015 at 10:03
  • 2
    @Pedram: Well where do you get that warning, and have you tried following its advice? As for checking the task for a faulted state - see the documentation for Task. Although if you await the task, the exception will be rethrown instead.
    – Jon Skeet
    Jul 20, 2015 at 10:10
  • The warning appears everywhere I have called the method RefreshContacts() If I add await before every call, does that mean I should change all the functions that run this function to async? Jul 20, 2015 at 12:53
  • 2
    @Pedram: Yes. It sounds like you fundamentally need to take a step back and think about what it means to refresh the contacts asynchronously...
    – Jon Skeet
    Jul 20, 2015 at 12:55
13

Change the method signature to Task<bool>. Then if your method is declared as async you can simple return a bool value. But as jon skeet said there are other, possibly better ways, to handle your szenario

 private async Task<bool> RefreshContacts()
    {
        Task refresh = Task.Run(() =>
        {
            try
            {
                // run the query
                      return true;
        }
        catch { return false;}      
}

PS: Another common issue you will possibly have is if you have a method without async. Then you can return Task.FromResult(true) like this:

 private Task<bool> RefreshContacts()
 {
     ....
    return Task.FromResult(true)
 }
3
  • 1
    When I change my catch to catch { return false; } after changing my return type to Task<bool> I got this warning message: "Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call." Jul 20, 2015 at 10:03
  • This message has nothing to do with the catch statement. Most likely, you don't have any asynchronous calls in the try part. In which case, there is no point in using async or Tasks. Jul 20, 2015 at 14:39
  • The first RefreshContacts version doesn't compile. Could you fix it? Feb 22, 2022 at 17:10
4

Sorry, but I think y'all misleading people here. See the Microsoft article, here.

Very simple example which shows how we can return a (scalar) value of bool, int or string type from a Task.

I am posting the C# code here, for posterity:

using System;
using System.Linq;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      Console.WriteLine(ShowTodaysInfo().Result);
   }

   private static async Task<string> ShowTodaysInfo()
   {
      string ret = $"Today is {DateTime.Today:D}\n" +
                   "Today's hours of leisure: " +
                   $"{await GetLeisureHours()}";
      return ret;
   }

   static async Task<int> GetLeisureHours()  
   {  
       // Task.FromResult is a placeholder for actual work that returns a string.  
       var today = await Task.FromResult<string>(DateTime.Now.DayOfWeek.ToString());  

       // The method then can process the result in some way.  
       int leisureHours;  
       if (today.First() == 'S')  
           leisureHours = 16;  
       else  
           leisureHours = 5;  

       return leisureHours;  
   }  
}
// The example displays output like the following:
//       Today is Wednesday, May 24, 2017
//       Today's hours of leisure: 5
// </Snippet >
0

It seems that you are trying to expose an asynchronous wrapper for a synchronous method. This is not recommended, and you can read the reasons here: Should I expose asynchronous wrappers for synchronous methods?

If you still insist on doing it, here is how it can be done:

private Task<bool> RefreshContactsAsync()
{
    return Task.Run(() =>
    {
        try
        {
            // Run the query
            return true;
        }
        catch
        {
            return false;
        }
    });
}

Notice the absence of the async and await keywords. We just use the Task.Run overload that takes a Func<TResult> parameter, and returns a Task<TResult>. The TResult in this case is of type bool.

What you should do instead? Simply make your RefreshContacts method synchronous:

private bool RefreshContacts()
{
    try
    {
        // Run the query
        return true;
    }
    catch
    {
        return false;
    }
}

...and wrap it in Task.Run at the calling site:

bool success = await Task.Run(() => RefreshContacts());

This way nobody is going to get the false impression that they are calling a genuinely asynchronous method (a method that doesn't run on a thread). The intent is clear: a synchronous method is offloaded to the ThreadPool, most probably for the reason of keeping the UI responsive.

0

google brought me here for a different question so I will answer what I was looking for in hopes that it helps someone else.

In the first example, the async keyword is missing creating a compiler error

        protected override Task<bool> ShouldMakeADecision()
        {
            return true;
        }

This will fail because you need to write the async keyword as shown below. You can see I put it after protected and before override.

        protected async override Task<bool> ShouldMakeADecision()
        {
            return true;
        }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.