4.5.14 异步方法

基于任务的异步模式 (TAP) 是 .NET Framework 中的一种异步模式。它基于命名空间中的 TaskTask<TResult> 类型,System.Threading.Tasks用于表示任意异步操作。

Async-Await是为与 TAP 一起使用而引入的新关键字。Async 修饰符用于指定方法、lambda 表达式或匿名方法是异步的。Await 运算符应用于异步方法中的任务,以暂停方法的执行,直到等待的任务完成

要求

  • 异步等待支持需要 .NET Framework 4.5 或更高版本

  • TAP支持需要 .NET Framework 4.0 或更高版本

  • MySQL 连接器/NET 6.9 或更高版本

方法

以下方法可与 TAP 或 Async-Await 一起使用。

  • 命名空间MySql.Data.Entity

    • 班级EFMySqlCommand

      • Task PrepareAsync()

      • Task PrepareAsync(CancellationToken)

  • 命名空间MySql.Data

    • 班级MySqlBulkLoader

      • Task<int> LoadAsync()

      • Task<int> LoadAsync(CancellationToken

    • 班级MySqlConnection

      • Task<MySqlTransaction> BeginTransactionAsync()

      • Task<MySqlTransaction> BeginTransactionAsync (CancellationToken)

      • Task<MySqlTransaction> BeginTransactionAsync(IsolationLevel)

      • Task<MySqlTransaction> BeginTransactionAsync (IsolationLevel , CancellationToken)

      • Task ChangeDatabaseAsync(string)

      • Task ChangeDatabaseAsync(string, CancellationToken)

      • Task CloseAsync()

      • Task CloseAsync(CancellationToken)

      • Task ClearPoolAsync(MySqlConnection)

      • Task ClearPoolAsync(MySqlConnection, CancellationToken)

      • Task ClearAllPoolsAsync()

      • Task ClearAllPoolsAsync(CancellationToken)

      • Task<MySqlSchemaCollection> GetSchemaCollection(string, string[])

      • Task<MySqlSchemaCollection> GetSchemaCollection(string, string[], CancellationToken)

    • 班级MySqlDataAdapter

      • Task<int> FillAsync(DataSet)

      • Task<int> FillAsync(DataSet, CancellationToken)

      • Task<int> FillAsync(DataTable)

      • Task<int> FillAsync(DataTable, CancellationToken)

      • Task<int> FillAsync(DataSet, string)

      • Task<int> FillAsync(DataSet, string, CancellationToken)

      • Task<int> FillAsync(DataTable, IDataReader)

      • Task<int> FillAsync(DataTable, IDataReader, CancellationToken)

      • Task<int> FillAsync(DataTable, IDbCommand, CommandBehavior)

      • Task<int> FillAsync(DataTable, IDbCommand, CommandBehavior, CancellationToken)

      • Task<int> FillAsync(int, int, params DataTable[])

      • Task<int> FillAsync(int, int, params DataTable[], CancellationToken)

      • Task<int> FillAsync(DataSet, int, int, string)

      • Task<int> FillAsync(DataSet, int, int, string, CancellationToken)

      • Task<int> FillAsync(DataSet, string, IDataReader, int, int)

      • Task<int> FillAsync(DataSet, string, IDataReader, int, int, CancellationToken)

      • Task<int> FillAsync(DataTable[], int, int, IDbCommand, CommandBehavior)

      • Task<int> FillAsync(DataTable[], int, int, IDbCommand, CommandBehavior, CancellationToken)

      • Task<int> FillAsync(DataSet, int, int, string, IDbCommand, CommandBehavior)

      • Task<int> FillAsync(DataSet, int, int, string, IDbCommand, CommandBehavior, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string, IDataReader)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, string, IDataReader, CancellationToken)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, IDbCommand, string, CommandBehavior)

      • Task<DataTable[]> FillSchemaAsync(DataSet, SchemaType, IDbCommand, string, CommandBehavior, CancellationToken)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, CancellationToken)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDataReader)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDataReader, CancellationToken)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDbCommand, CommandBehavior)

      • Task<DataTable> FillSchemaAsync(DataTable, SchemaType, IDbCommand, CommandBehavior, CancellationToken)

      • Task<int> UpdateAsync(DataRow[])

      • Task<int> UpdateAsync(DataRow[], CancellationToken)

      • Task<int> UpdateAsync(DataSet)

      • Task<int> UpdateAsync(DataSet, CancellationToken)

      • Task<int> UpdateAsync(DataTable)

      • Task<int> UpdateAsync(DataTable, CancellationToken)

      • Task<int> UpdateAsync(DataRow[], DataTableMapping, CancellationToken)

      • Task<int> UpdateAsync(DataSet, string)

      • Task<int> UpdateAsync(DataSet, string, CancellationToken)

    • 班级MySqlHelper

      • Task<DataRow> ExecuteDataRowAsync(string, string, params MySqlParameter[])

      • Task<DataRow> ExecuteDataRowAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(string, string, params MySqlParameter[])

      • Task<int> ExecuteNonQueryAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<DataSet> ExecuteDatasetAsync(string, string)

      • Task<DataSet> ExecuteDatasetAsync(string, string, CancellationToken)

      • Task<DataSet> ExecuteDatasetAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string)

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string, CancellationToken)

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<DataSet> ExecuteDatasetAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

      • Task UpdateDataSetAsync(string, string, DataSet, string)

      • Task UpdateDataSetAsync(string, string, DataSet, string, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, MySqlTransaction, string, MySqlParameter[], bool)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, MySqlTransaction, string, MySqlParameter[], bool, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string)

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string)

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string, CancellationToken)

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string, params MySqlParameter[])

      • Task<MySqlDataReader> ExecuteReaderAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<MySqlDataReader> ExecuteReaderAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(string, string)

      • Task<object> ExecuteScalarAsync(string, string, CancellationToken)

      • Task<object> ExecuteScalarAsync(string, string, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(string, string, CancellationToken, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(MySqlConnection, string)

      • Task<object> ExecuteScalarAsync(MySqlConnection, string, CancellationToken)

      • Task<object> ExecuteScalarAsync(MySqlConnection, string, params MySqlParameter[])

      • Task<object> ExecuteScalarAsync(MySqlConnection, string, CancellationToken, params MySqlParameter[])

    • 班级MySqlScript

      • Task<int> ExecuteAsync()

      • Task<int> ExecuteAsync(CancellationToken)

除了上面列出的方法外,以下是从 .NET Framework 继承的方法:

  • 命名空间MySql.Data.Entity

    • 班级EFMySqlCommand

      • Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehaviour, CancellationToken)

      • Task<int> ExecuteNonQueryAsync()

      • Task<int> ExecuteNonQueryAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync()

      • Task<DbDataReader> ExecuteReaderAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour, CancellationToken)

      • Task<object> ExecuteScalarAsync()

      • Task<object> ExecuteScalarAsync(CancellationToken)

  • 命名空间MySql.Data

    • 班级MySqlCommand

      • Task<DbDataReader> ExecuteDbDataReaderAsync(CommandBehaviour, CancellationToken)

      • Task<int> ExecuteNonQueryAsync()

      • Task<int> ExecuteNonQueryAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync()

      • Task<DbDataReader> ExecuteReaderAsync(CancellationToken)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour)

      • Task<DbDataReader> ExecuteReaderAsync(CommandBehaviour, CancellationToken)

      • Task<object> ExecuteScalarAsync()

      • Task<object> ExecuteScalarAsync(CancellationToken)

    • 班级MySqlConnection

      • Task OpenAsync()

      • Task OpenAsync(CancellationToken)

    • 班级MySqlDataReader

      • Task<T> GetFieldValueAsync<T>(int)

      • Task<T> GetFieldValueAsync<T>(int, CancellationToken)

      • Task<bool> IsDBNullAsync(int)

      • Task<bool> IsDBNullAsync(int, CancellationToken)

      • Task<bool> NextResultAsync()

      • Task<bool> NextResultAsync(CancellationToken)

      • Task<bool> ReadAsync()

      • Task<bool> ReadAsync(CancellationToken)

例子

以下 C# 代码示例演示了如何使用异步方法:

在此示例中,方法具有async修饰符,因为进行的方法await调用适用于方法LoadAsync。该方法返回一个 Task对象,该对象包含有关等待方法的结果的信息。返回Task就像有一个 void 方法,但async void如果你的方法不是像事件这样的顶级访问方法,你就不应该使用。

public async Task BulkLoadAsync()
{
  MySqlConnection myConn = new MySqlConnection("MyConnectionString");
  MySqlBulkLoader loader = new MySqlBulkLoader(myConn);

  loader.TableName       = "BulkLoadTest";
  loader.FileName        = @"c:\MyPath\MyFile.txt";
  loader.Timeout         = 0;

  var result             = await loader.LoadAsync();
}

在这个例子中,“async void”方法与“await” ExecuteNonQueryAsync方法一起使用,以对应按钮的 onclick 事件。这就是该方法不返回 Task.

private async void myButton_Click()
{
  MySqlConnection myConn = new MySqlConnection("MyConnectionString");
  MySqlCommand proc      = new MySqlCommand("MyAsyncSpTest", myConn);

  proc.CommandType       = CommandType.StoredProcedure;

  int result             = await proc.ExecuteNonQueryAsync();
}