Click here to Skip to main content
15,921,779 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Back online Pin
Foothill27-Oct-16 6:26
professionalFoothill27-Oct-16 6:26 
RantAsync and await the end of the call stack. Pin
Brady Kelly26-Oct-16 6:12
Brady Kelly26-Oct-16 6:12 
GeneralRe: Async and await the end of the call stack. Pin
TheGreatAndPowerfulOz26-Oct-16 6:23
TheGreatAndPowerfulOz26-Oct-16 6:23 
GeneralRe: Async and await the end of the call stack. Pin
Brady Kelly26-Oct-16 6:34
Brady Kelly26-Oct-16 6:34 
GeneralRe: Async and await the end of the call stack. Pin
TheGreatAndPowerfulOz26-Oct-16 7:20
TheGreatAndPowerfulOz26-Oct-16 7:20 
GeneralRe: Async and await the end of the call stack. Pin
Marc Clifton26-Oct-16 8:24
mvaMarc Clifton26-Oct-16 8:24 
GeneralRe: Async and await the end of the call stack. Pin
Brady Kelly26-Oct-16 9:30
Brady Kelly26-Oct-16 9:30 
GeneralRe: Async and await the end of the call stack. PinPopular
Richard Deeming26-Oct-16 9:44
mveRichard Deeming26-Oct-16 9:44 
Marc Clifton wrote:
ok, when the continuation returns here, then it returns here, then it returns here...but what happens when an exception is thrown...

async and await make it easier to reason about that sort of code. For the most part, it's basically the same as synchronous code, except it doesn't block the thread waiting for external calls to complete.

A simple example:

Synchronous code:
C#
Ping ping = new Ping();
try
{
    // Block the calling thread waiting for the reply...
    PingReply reply = ping.Send(address);
    
    // Now do something with the reply...
}
catch (PingException)
{
    // Handle the exception...
}


EAP code:
C#
Ping ping = new Ping();

// Can't declare the handler on one line, since you need to remove it from within itself...
PingCompletedEventHandler handler = null;
handler = (sender, e) =>
{
    ping.PingCompleted -= handler;
    
    if (e.Error != null)
    {
        PingException ex = e.Error as PingException;
        if (ex == null) throw new TargeInvocationException("Error", e.Error);
        
        // Handle the exception...
    }
    else
    {
        PingReply reply = e.Reply;
        
        // Now do something with the reply...
    }
};

// Need to wire up the handler before calling the method...
ping.PingCompleted += handler;

// The calling thread will not be blocked...
ping.SendAsync(address);


Continuation passing code:
C#
Ping ping = new Ping();

// The calling thread will not be blocked...
Task<PingReply> pingTask = ping.SendPingAsync();

// But everything else has to move to a continuation...
Task resultTask = pingTask.ContinueWith(task =>
{
    if (task.IsFauled)
    {
        AggregateException ex = task.Exception;
        ex.Handle(error =>
        {
            PingException pingEx = error as PingException;
            if (pingEx == null) return false;
            
            // Handle the exception...
            return true;
        });
    }
    else
    {
        PingReply reply = task.Result;
        
        // Now do something with the reply...
    }
});


Async code:
C#
Ping ping = new Ping();
try
{
    // The calling thread will not be blocked...
    PingReply reply = await ping.SendPingAsync(address);
    
    // Now do something with the reply...
}
catch (PingException)
{
    // Handle the exception...
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Async and await the end of the call stack. Pin
Marc Clifton26-Oct-16 10:49
mvaMarc Clifton26-Oct-16 10:49 
GeneralRe: Async and await the end of the call stack. Pin
Mark_Wallace26-Oct-16 23:09
Mark_Wallace26-Oct-16 23:09 
GeneralRe: Async and await the end of the call stack. Pin
Munchies_Matt26-Oct-16 8:53
Munchies_Matt26-Oct-16 8:53 
GeneralRe: Async and await the end of the call stack. Pin
Brady Kelly26-Oct-16 9:38
Brady Kelly26-Oct-16 9:38 
GeneralRe: Async and await the end of the call stack. Pin
Sander Rossel26-Oct-16 9:14
professionalSander Rossel26-Oct-16 9:14 
GeneralRe: Async and await the end of the call stack. Pin
Brady Kelly26-Oct-16 9:41
Brady Kelly26-Oct-16 9:41 
GeneralRe: Async and await the end of the call stack. Pin
Shuqian Ying26-Oct-16 11:53
Shuqian Ying26-Oct-16 11:53 
GeneralRe: Async and await the end of the call stack. Pin
Brady Kelly26-Oct-16 17:24
Brady Kelly26-Oct-16 17:24 
GeneralRe: Async and await the end of the call stack. Pin
Shuqian Ying26-Oct-16 20:42
Shuqian Ying26-Oct-16 20:42 
GeneralMissing SA Answers Pin
SHugeNF26-Oct-16 5:55
SHugeNF26-Oct-16 5:55 
GeneralRe: Missing SA Answers Pin
Abhinav S26-Oct-16 6:10
Abhinav S26-Oct-16 6:10 
GeneralRe: Missing SA Answers Pin
Nelek26-Oct-16 7:33
protectorNelek26-Oct-16 7:33 
RantWelders Pin
Munchies_Matt26-Oct-16 5:49
Munchies_Matt26-Oct-16 5:49 
GeneralRe: Welders Pin
Sander Rossel26-Oct-16 5:56
professionalSander Rossel26-Oct-16 5:56 
GeneralRe: Welders PinPopular
PIEBALDconsult26-Oct-16 5:57
mvePIEBALDconsult26-Oct-16 5:57 
GeneralRe: Welders Pin
Munchies_Matt26-Oct-16 6:04
Munchies_Matt26-Oct-16 6:04 
GeneralRe: Welders Pin
Brady Kelly26-Oct-16 6:44
Brady Kelly26-Oct-16 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.