在异步调用C#中包装一个同步方法 [英] Wrap a synchronous method in Asynchronous call c#

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

问题描述

我希望将同步方法包装到异步代码中,以便将这些方法公开为异步方法-原因是我不想在同步方法内复制代码,因为我希望它是DRY并且已经知道其功能同步代码有效-无需管理单独的方法.但是我想要异步方法.我至少了解异步方法的用途以及为什么要使用它们.

考虑以下代码:

public int AddValues(int a, int b)
{
  // some really boring or extremely long DRY method code 
  return a + b;
}

添加如下所示的异步调用方法 节省编写代码的时间;确保内部代码的功能与同步代码相同. 我可以还是应该这样做?:

public async task AddValuesAsync(int a, int b)
{
  return await Task.FromResult(AddValues(a,b));
}

这是我的另一个问题,也许是我的答案; 两次调用Asynch方法也将调用sync方法-由于它在异步后面,因此我会得到一个跨线程异常?正确吗?

那么在这种情况下我该怎么办-如何使其保持干燥并保持简单? 我可以将sync方法设为私有,并强制对该方法的所有调用都是Async ..

如果您说我的示例代码是一种不好的方法,请给出一个简短的原因,并给出一个简单的示例来说明它是一种好的方法.以及关于什么是最佳方法以及如何做到这一点的任何建议? /p>

我看过这里 正在包装同步调用Task.Run()以使其异步受益? 但这似乎并不能很清楚地回答我想知道的事情.

编辑

对于其他正在寻找的人-除了此处标记的答案以外,还有指向示例的答案以及更重要的原因的信息.

我应该为同步方法公开异步包装吗?

哪个最好的方法是将同步代码包装到异步方法中?

解决方案

我之所以这样问,是因为我想知道使同步方法在类中尽可能具有DRY的异步方法的正确方法是什么.

正确的方法是不要" .

更重要的是,您的API不应该说谎.如果他们正在执行同步工作,则应该具有同步签名.

I am looking to wrap synchronous methods into asynchronous code in order to expose those methods as asynchronous - the reason is I do not want to duplicate the code inside the synchrnous methods as I would like it to be DRY and already knowing the functionality of the syunchronous code works - no need to manage separate method as well. However I would like asynchronous methods. I understand the purpose of asynchronous methods at least what they do and why I would want to use them.

Consider the following code:

public int AddValues(int a, int b)
{
  // some really boring or extremely long DRY method code 
  return a + b;
}

Adding an Asynchronous Calling Method like below saves time writing code ; Ensures the functionality of the inner code is the same as Synchronous code. Could I or Should I do this?:

public async task AddValuesAsync(int a, int b)
{
  return await Task.FromResult(AddValues(a,b));
}

And here is my other question and maybe an answer to me; Calling the Asynch method twice will also call the sync method - and since it is behind the async I can get a cross thread exception ? IS that correct.

So what should I do in this situation - how to keep it DRY and keep it simple? I could make the sync method private and force all calls to the method be Async ..

If you say my example code is a bad way to do it, please give a short reason why and a simple example of a good way to do it ..and any suggestions on what is best and how to do this ?

I have looked here Is wrapping a synchronous call in a Task.Run() to make it asynchronous beneficial? but that does not seem to answer very clearly , what I would like to know.

EDIT

For others who are looking - aside from the answer marked here is info that points to the answer with examples and also more importantly why.

Should I expose asynchronous wrappers for synchronous methods?

Which way is best for wrapping synchronous code into an asynchronous method?

解决方案

I am asking this because I would like to know what the proper way is to make a synchronous methods to have asynchronous versions in a class and as DRY as possible.

The proper way is "don't".

More to the point, your API's shouldn't lie. If they're doing synchronous work, they should have a synchronous signature.

这篇关于在异步调用C#中包装一个同步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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