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

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

问题描述

我正在写一个C#.NET 4.5库做普通的SQL数据库操作(备份,恢复,执行脚本等)。我想对每个操作同步和异步功能,该库将在控制台和GUI应用中使用,但我不想到处复制code。所以当我看到它,我有两个选择:


  1. 写code,做了同步功能的工作,然后就敷在一个任务异步功能,像这样:

     公共无效的backupdb(字符串服务器,字符串DB)
    {
        //做所有的工作和长时间运行的操作这里
    }公共异步任务BackupDBAsync(字符串服务器,字符串DB)
    {
        等待Task.Factory.StartNew(()=>的某个backupdb(服务器,DB))ConfigureAwait(假)。
    }


  2. 写code,做在异步功能的工作,并利用.Wait从同步函数调用它():

     公共异步任务BackupDBAsync(字符串服务器,字符串DB)
    {
        //做所有的工作和长时间运行的操作这里,异步。
    }公共无效的backupdb(字符串服务器,字符串DB)
    {
        BackupDBAsync(服务器,DB).Wait(); //执行将在这里等待,直到异步功能完全结束。
    }


是一种选择优于其他?是一个最好的做法?还是有其他(更好)的替代方案?

我知道,一个警告使用.Wait()是所有在异步函数的await语句必须使用.ConfigureAwait(假),以避免死锁(<一个href=\"http://stackoverflow.com/questions/9343594/how-to-call-asynchronous-method-from-synchronous-method-in-c\">as这里讨论),但是因为我在写永远不会需要访问库我是安全的UI或WebContext做到这一点。

我会注意到过的SQL库通常还具有可用于同步和异步功能,因此,如果做的工作在同步功能,我会叫他们的同步功能,而且如果做的工作在异步功能,我会叫他们的异步函数。

思想/建议AP preciated。

- 编辑:我也张贴了这个问题,<一个href=\"http://social.msdn.microsoft.com/Forums/en-US/async/thread/7bc69ba9-64b0-447e-b200-ca47c9fdce43\">on这里的MSDN论坛的尝试,并得到一个正式的答复MS -


解决方案

  

我想为每个操作同步和异步功能,该库将在控制台和GUI应用中使用,但我不想到处复制code。


最好的答案是:没有。

斯蒂芬Toub有两个优秀的博客文章关于这个主题:

他建议公开异步方法异步和同步方式为同步。如果您需要公开两个,然后在私人(同步)方法封装常用的功能,并复制异步/同步的差异。

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

  2. 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?

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.

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.

Thoughts/suggestions are appreciated.

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

解决方案

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.

The best answer is: don't.

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天全站免登陆