Alan2
Alan2

Reputation: 24562

How can I make an async Task return true or false?

I am calling a task like this in my method:

if (Settings.mode.IsPractice() && App.practiceRunning != true)
{
   // I want to call this method and check if it returned true or false
   await IsPractice();
}

private async Task IsPractice()
{
    // I want to return true or false from here
}

How can I return from the method based on the value of the IsPractice() returning a true or false? Looks like the async method returns just a Task but I need to know if it ran and returned a true or a false.

Upvotes: 3

Views: 3503

Answers (3)

Tony Tom
Tony Tom

Reputation: 1583

 private static async Task<bool> IsPractice()
    {
     return true;   
    }

and in async method receive like this

bool x =  await IsPractice();

Upvotes: 2

Saif
Saif

Reputation: 2679

just add <bool>

private async Task<bool> IsPractice()
{
    return true;
}

or just use async bool

private async bool IsPractice()
{
    return true;
}

Upvotes: 1

Christoph Herold
Christoph Herold

Reputation: 1809

Use the generic Task type:

if (Settings.mode.IsPractice() && App.practiceRunning != true)
{
    if (await IsPractice()) {
        // Do something here
    }
}

private async Task<bool> IsPractice()
{
    return true;
}

Upvotes: 3

Related Questions