2

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.

2
  • Is there a reason you can't just put the second task's body in the first task? ie. await Task.Factory.StartNew( ()=> { try { scannedItem = getScannedItem(); getResultsScannedItem = result( scannedItem ); } ... Commented Jul 13, 2015 at 21:33
  • @clcto I tried putting it inside the first one, but I get the error: "The 'await' operator can only be used when its containing lambda expression is marked with the 'async' modifier" Commented Jul 13, 2015 at 21:36

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Just taking out the try and catch is worth a +1 in itself.
I would also add a note about the void return type. You should never return void on an async methode except for event handlers. public async void getResult () should become public async Task getResult ()
Yeah .. should return Task
How would I go about setting that up with my code? @tequilaslammer
As I have written, just replace void with Task.
1
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.