I recently wrote the following code:
Task<T> ExecAsync<T>( string connectionString, SqlCommand cmd, Func<SqlCommand, T> resultBuilder, CancellationToken cancellationToken = default(CancellationToken) )
{
var tcs = new TaskCompletionSource<T>();
SqlConnectionProvider p;
try
{
p = GetProvider( connectionString );
Task<IDisposable> openTask = p.AcquireConnectionAsync( cmd, cancellationToken );
openTask
.ContinueWith( open =>
{
if( open.IsFaulted ) tcs.SetException( open.Exception.InnerExceptions );
else if( open.IsCanceled ) tcs.SetCanceled();
else
{
var execTask = cmd.ExecuteNonQueryAsync( cancellationToken );
execTask.ContinueWith( exec =>
{
if( exec.IsFaulted ) tcs.SetException( exec.Exception.InnerExceptions );
else if( exec.IsCanceled ) tcs.SetCanceled();
else
{
try
{
tcs.SetResult( resultBuilder( cmd ) );
}
catch( Exception exc ) { tcs.TrySetException( exc ); }
}
}, TaskContinuationOptions.ExecuteSynchronously );
}
} )
.ContinueWith( _ =>
{
if( !openTask.IsFaulted ) openTask.Result.Dispose();
}, TaskContinuationOptions.ExecuteSynchronously );
}
catch( Exception ex )
{
tcs.SetException( ex );
}
return tcs.Task;
}
This works as intended. The same code written with async/await is (obviously) simpler:
async Task<T> ExecAsync<T>( string connectionString, SqlCommand cmd, Func<SqlCommand, T> resultBuilder, CancellationToken cancellationToken = default(CancellationToken) )
{
SqlConnectionProvider p = GetProvider( connectionString );
using( IDisposable openTask = await p.AcquireConnectionAsync( cmd, cancellationToken ) )
{
await cmd.ExecuteNonQueryAsync( cancellationToken );
return resultBuilder( cmd );
}
}
I had a quick look at the generated IL for the 2 versions: the async/await is bigger (not a surprise) but I was wondering if the async/await code generator analyses the fact that a continuation is actually synchronous to use TaskContinuationOptions.ExecuteSynchronously
where it can... and I failed to find this in the IL generated code.
If anyone knows this or have any clue about it, I'd be pleased to know!