在异步调用C#中包装一个同步方法

编程入门 行业动态 更新时间:2024-10-25 08:15:21
本文介绍了在异步调用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#中包装一个同步方法

本文发布于:2023-11-23 10:30:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621114.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!