jipot
jipot

Reputation: 94

Multiple await's inside an async method

I have a method

public async void getResult ()

That relies on 2 await responses inside of it.

This is the 1st: await:

await System.Threading.Tasks.Task.Factory.StartNew (() => { 
                    try 
                    {
                        scannedItem = getScannedItem(); 
                    }
                    catch (Exception exe) 
                    { }

And the 2nd await await call that uses scannedItem from above

await System.Threading.Tasks.Task.Factory.StartNew (() => { 
                    try
                    {
                        getResultsScannedItem = results(ScannedItem); 
                    }
                    catch (Exception exe) 
                    { }

I want to have this all execute from one button. However, the second await never gets executed. Can I even have two awaits inside a method? Bit confused on how to go about doing this.

Upvotes: 2

Views: 5784

Answers (2)

Jack Le
Jack Le

Reputation: 156

public async void getResult ()
{
     ScannedItem scannedItem;
     try 
     {
        scannedItem = await getScannedItem(); 
        getResultsScannedItem = await results(scannedItem);   
     } catch (Exception exe) { }
}

You should add "async" to getScannedItem and results functions. Hope it helps.

Upvotes: 1

scartag
scartag

Reputation: 17680

Yes you can have multiple awaits inside a single method .. but these don't look like awaitable Tasks .. you are simply wrapping them up with a Task Factory.

You also seem to be swallowing whatever exception is probably happening within the code.

Try changing to:

await Task.Run(() => scannedItem = getScannedItem());

await Task.Run(() => getResultsScannedItem = results(ScannedItem));

Step through it with the debugger and see if its running or an exception is happening.

Upvotes: 12

Related Questions