Search code examples
c#sql-servercancellationtokensourcecancellation-token

How i can cancel a ExecuteReaderAsync with CancellationToken


I have a long running query , when I try to abort the query with CancellationToken nothing happens.

using (var cmd = connection.CreateCommand())
{
    cmd.CommandText = contrat.StoredProcedureName;
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandTimeout = 2000;
    AddParameters(cmd, contrat.ParametresCommand);

    using (var reader = await cmd.ExecuteReaderAsync(Token))
    {
        return contrat.ConvertAsync(reader);
    }    
}                    

Solution

  • You need to set a timeout value on the token, or manually cancel the token.