I\'d like to write a bunch of methods querying the Oracle Database in the async/await way. Since ODP.NET seems to support neither awaitable *Async methods nor Begin/EndOpera
As long as I know Oracle ODP is a sync wrapper for an async library. I found this post as I just wondering the same: Will the introduction of an async pattern for Oracle ODP calls improve performance? (I am using WCF on IIS NET TCP).
But, as it have been said, as long as the introduction of the async pattern is done creating a new Task and the calling thread is already from the thread pool, no improvement can't be done, and it will be just a overhead.
I'm using this
public static class TaskHelper
{
public async static Task<T> AsAsync<T>(Func<T> function, TaskCreationOptions taskCreationOptions = TaskCreationOptions.None)
{
return await Task.Factory.StartNew(function, taskCreationOptions);
}
public async static Task AsAsync(Action action, TaskCreationOptions taskCreationOptions = TaskCreationOptions.None)
{
await Task.Factory.StartNew(action, taskCreationOptions);
}
}
Any synchronous function can be made asynchronous and you can await of it.
you can always use a Task.Factory.StartNew with the TaskCreationOptions.LongRunning, so that .NET will create a new thread rather than using a threadpool thread. Below is the manual asynchronous code that you can apply to your operation.
private static void ManualAsyncOperation()
{
Task<string> task = Task.Factory.StartNew(() =>
{
Console.WriteLine("Accessing database .....");
//Mimic the DB operation
Thread.Sleep(1000);
return "Hello wolrd";
},TaskCreationOptions.LongRunning);
var awaiter =task.GetAwaiter();
awaiter.OnCompleted(() =>
{
try
{
var result = awaiter.GetResult();
Console.WriteLine("Result: {0}", result);
}
catch (Exception exception)
{
Console.WriteLine("Exception: {0}",exception);
}
});
Console.WriteLine("Continuing on the main thread and waiting for the result ....");
Console.WriteLine();
Console.ReadLine();
}