C#有异步函数调用同步函数或同步函数调用异步函数 [英] C# have async function call synchronous function or synchronous function call async function

查看:25
本文介绍了C#有异步函数调用同步函数或同步函数调用异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 C# .Net 4.5 库,用于执行常见的 sql 数据库操作(备份、恢复、执行脚本等).我希望每个操作都有同步和异步函数,因为控制台和 GUI 应用程序都将使用这个库,但我不想在任何地方复制代码.所以在我看来,我有两个选择:

I'm writing a C# .Net 4.5 library for doing common sql database operations (backup, restore, execute script, etc.). I want to have both synchronous and asynchronous functions for each operation, as this library will be used by both console and GUI apps, but I don't want to duplicate code everywhere. So as I see it, I have two options:

  1. 在同步函数中编写完成工作的代码,然后将其包装在异步函数的任务中,如下所示:

  1. Write the code that does the work in a synchronous function, and then just wrap it in a task for the async function, like so:

public void BackupDB(string server, string db)  
{  
    // Do all of the work and long running operation here  
}

public async Task BackupDBAsync(string server, string db)  
{  
    await Task.Factory.StartNew(() => BackupDB(server, db)).ConfigureAwait(false);  
}

  • 编写在异步函数中完成工作的代码,并使用 .Wait() 从同步函数中调用它:

  • Write the code that does the work in an asynchronous function, and call it from a synchronous function using .Wait():

    public async Task BackupDBAsync(string server, string db)  
    {  
        // Do all of the work and long running operation here, asynchronously.  
    }
    
    public void BackupDB(string server, string db)  
    {  
        BackupDBAsync(server, db).Wait(); // Execution will wait here until async function finishes completely.  
    }
    

  • 一种选择是否比另一种更好?一个是最佳实践吗?或者还有其他(更好的)替代方案吗?

    Is one option better than the other? Is one a best practice? Or are there any other (better) alternatives?

    我知道使用 .Wait() 的一个警告是异步函数中的所有 await 语句都必须使用 .ConfigureAwait(false) 以避免死锁(如此处讨论),但因为我正在编写一个永远不需要访问用户界面的库或 WebContext 我可以安全地这样做.

    I know that one caveat to using .Wait() is that all of the await statements in the async function have to use .ConfigureAwait(false) to avoid deadlocks (as discussed here), but since I'm writing a library that will never need to access the UI or WebContext I am safe to do that.

    我还要注意的是,SQL 库通常也有同步和异步函数都可以使用,所以如果在同步函数中进行工作,我会调用它们的同步函数,如果在异步中进行工作函数,我会调用他们的异步函数.

    I'll note too that the SQL library typically also has both synchronous and async functions that can be used, so if doing the work in the sync function, I would call their sync function, and if doing the work in the async function, I would call their async function.

    感谢您的想法/建议.

    -- 我也发布了这个问题 在此处的 MSDN 论坛上 尝试获得 MS 的官方回复 --

    -- edit: I've also posted this question on the MSDN forums here to try and get an official MS response --

    推荐答案

    我希望每个操作都有同步和异步函数,因为控制台和 GUI 应用程序都将使用这个库,但我不想到处重复代码.

    I want to have both synchronous and asynchronous functions for each operation, as this library will be used by both console and GUI apps, but I don't want to duplicate code everywhere.

    最好的答案是:不要.

    Stephen Toub 有两篇关于这个主题的优秀博文:

    Stephen Toub has two excellent blog posts on this topic:

    他建议将异步方法公开为异步,将同步方法公开为同步.如果您需要同时公开两者,则将通用功能封装在私有(同步)方法中,并复制异步/同步差异.

    He recommends exposing asynchronous methods as asynchronous, and synchronous methods as synchronous. If you need to expose both, then encapsulate common functionality in private (synchronous) methods, and duplicate the async/sync differences.

    这篇关于C#有异步函数调用同步函数或同步函数调用异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆