Skip to content

Cancel async functions without throw #4450

Discussion options

You must be logged in to vote

I think there is actually room for the compiler to optimize wihout doing language changes:

Consider the following super simple async function:

using System;
using System.Threading.Tasks;

public class C {
    public async Task<int> M(bool b) {
        await Task.Yield();
        if (b)
            throw new Exception();
        return 42;
    }
}

This translates into a state machine that looks like this:

using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

public class C
{
    private struct Md__0 : IAsyncStateMachine
    {
        public int _1__state;

        public AsyncTaskMethodBuilder<int> t__builder;

        public bool b;

        private YieldAwai…

Replies: 6 comments 38 replies

Comment options

You must be logged in to vote
4 replies
@timcassell
Comment options

@CyrusNajmabadi
Comment options

@theunrepentantgeek
Comment options

@bernd5
Comment options

Comment options

You must be logged in to vote
6 replies
@yaakov-h
Comment options

@timcassell
Comment options

@AartBluestoke
Comment options

@timcassell
Comment options

@sharwell
Comment options

Answer selected by timcassell
Comment options

You must be logged in to vote
4 replies
@CyrusNajmabadi
Comment options

@sharwell
Comment options

@timcassell
Comment options

@sharwell
Comment options

Comment options

You must be logged in to vote
10 replies
@timcassell
Comment options

@jnm2
Comment options

@AartBluestoke
Comment options

@HaloFour
Comment options

@CyrusNajmabadi
Comment options

Comment options

You must be logged in to vote
14 replies
@sharwell
Comment options

@timcassell
Comment options

@sharwell
Comment options

@timcassell
Comment options

@timcassell
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet