Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 466
jipot Avatar asked May 09 '26 10:05

jipot


1 Answers

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.

like image 91
scartag Avatar answered May 10 '26 22:05

scartag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!