OpenAsync(CancellationToken cancellationToken)

This method returns a new Task-based asynchronous version of OracleConnection.Open.

Declaration

// C#
public override Task OpenAsync(CancellationToken cancellationToken);

Parameters

Return Value

Task object representing the asynchronous operation immediately without blocking the calling thread for the whole duration of opening connection.

Implements

DbConnection

Exceptions

Remarks

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();
    }
  }
}

See Also: