简体   繁体   English

Oracle Client与基于任务的异步模式(异步/等待)

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

I'd like to write a bunch of methods querying the Oracle Database in the async/await way. 我想编写一堆以异步/等待方式查询Oracle数据库的方法。 Since ODP.NET seems to support neither awaitable *Async methods nor Begin/EndOperationName pairs, what options do I have to implement this manually? 由于ODP.NET似乎既不支持等待的* Async方法也不支持Begin / EndOperationName对,因此我必须手动执行哪些选项?

All examples for I/O-intensive async methods I've seen so far call only other async methods from the .NET library, but no light is shed on the way the context switching is done internally. 到目前为止,我已经看到了所有I / O密集型异步方法的示例,它们仅调用.NET库中的其他异步方法,但是在内部进行上下文切换的方式上丝毫不为过。 The documentation says that in these cases no separate thread is used and the multithreading overhead is apparently worth only for CPU-intensive operations. 该文档说,在这些情况下,不使用单独的线程,并且多线程开销显然仅对CPU密集型操作有价值。 So I guess using Task.Run() is not an option, or am I wrong? 所以我想使用Task.Run()不是一个选择,否则我错了吗?

As long as I know Oracle ODP is a sync wrapper for an async library. 只要我知道Oracle ODP是异步库的同步包装器即可。 I found this post as I just wondering the same: Will the introduction of an async pattern for Oracle ODP calls improve performance? 我发现这个帖子的时候,我只是想知道一个问题:为Oracle ODP调用引入异步模式是否会提高性能? (I am using WCF on IIS NET TCP). (我在IIS NET TCP上使用WCF)。

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. 但是,正如已经说过的,只要完成了异步模式的创建,就创建了一个新Task,并且调用线程已经在线程池中,就无法进行任何改进,这只是开销。

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. 您始终可以将Task.Factory.StartNewTaskCreationOptions.LongRunning一起使用,以便.NET将创建一个新线程,而不是使用线程线程。 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();

        }

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. 任何同步功能都可以设为异步,您可以等待它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Telerik中基于任务的异步模式支持 - Task-based Asynchronous Pattern support in Telerik 具有基于任务的异步模式的UDP侦听器 - UDP listener with task-based asynchronous pattern 使用.NET 4.5基于任务的异步操作的WCF服务客户端,等待永远不会返回 - WCF Service Client with .NET 4.5 Task-Based Async Operations, await never returns 在双工WCF服务的客户端中使用基于任务的异步模式时出错 - Error when I use the Task-based Asynchronous Pattern in the client of duplex WCF service 异步单元测试 - async/await 与 Task.Result - Asynchronous Unit Testing - async/await vs. Task.Result C#中基于任务的异步方法的超时模式 - Timeout pattern on task-based asynchronous method in C# 等待/异步与“经典”异步(回调) - await/async vs. “classic” asynchronous (callbacks) 从异步编程模型(APM)迁移到基于任务的异步模式(TAP) - Moving from Asynchronous Programming Model (APM) to Task-based Asynchronous Pattern (TAP) 设计:任务异步模式(具有等待/异步功能的TAP),具有信令的线程与其他线程结构 - Design: Task-Asynchronous Pattern (TAP with await / async), vs threads with signalling vs other thread structures 通过基于任务的异步模式 (TAP) 接触的 Windows 服务作业监视器 - Windows Service Job Monitor via Touch of Task-based Asynchronous Pattern (TAP)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM