OpenAsync(CancellationToken cancellationToken)
This method returns a new Task-based asynchronous version of OracleConnection.Open.
Declaration
// C# public override Task OpenAsync(CancellationToken cancellationToken);
Parameters
-
cancellationToken- The input cancellation token which can be used by the application to cancel the task before connection timeout occurs.
Return Value
Task object representing the asynchronous operation immediately without blocking the calling thread for the whole duration of opening connection.
Implements
DbConnection
Exceptions
-
ObjectDisposedException- The object is already disposed. -
InvalidOperationException- The connection is already opened or the connection string is null or empty.
Remarks
-
After calling
OpenAsync,OracleConnection.Statewill returnConnectinguntil the returnedTaskis completed. Then, if the connection was successful,Statewill returnOpen. If the connection fails,Statewill returnClosed. -
If the connection timeout time elapses without successfully connecting, the returned
Taskwill be marked as faulted with an Exception.
Example
using Oracle.ManagedDataAccess.Client;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncApp
{
class AsyncDemo
{
static async Task Main()
{
string connectionString = "User Id=HR; Password=<PASSWORD>; Data Source=oracle;";
OracleConnection oc = new OracleConnection(connectionString);
// Open a connection asynchronously
Task task = oc.OpenAsync(CancellationToken.None);
// Execute an operation while the connection is being opened asynchronously
Console.WriteLine("Opening a connection asynchronously.");
// wait for the connection to be opened
await task;
Console.WriteLine("Connection opened successfully");
oc.Close();
}
}
}