Starting with a synchronous I/O bound method (below), how do I make it asynchronous using async/await?
public int Iobound(SqlConnection conn, SqlTransaction tran)
{
// this stored procedure takes a few seconds to complete
SqlCommand cmd = new SqlCommand("MyIoboundStoredProc", conn, tran);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter returnValue = cmd.Parameters.Add("ReturnValue", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.ExecuteNonQuery();
return (int)returnValue.Value;
}
MSDN examples all presume the preexistence of an *Async method and offer no guidance for making one yourself for I/O-bound operations.
I could use Task.Run() and execute Iobound() within that new Task, but new Task creation is discouraged since the operation is not CPU-bound.
I'd like to use async/await but I'm stuck here on this fundamental problem of how to proceed with the conversion of this method.
ExecuteNonQueryAsync()
? – Camilo Terevintousing
on your SqlCommand... – slugster{
(on new line or not) is personal preference and changing it does not add any value. – Evk