Oracle Client vs. Task-based Asynchronous Pattern (async/await)

前端 未结 3 1224
闹比i
闹比i 2021-01-12 07:53

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

相关标签:
3条回答
  • 2021-01-12 07:57

    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.

    0 讨论(0)
  • 2021-01-12 08:09

    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.

    0 讨论(0)
  • 2021-01-12 08:21

    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();
    
            }
    
    0 讨论(0)
提交回复
热议问题