如何在异步和同步代码之间切换? [英] How do I switch between asynchronous and synchronous code?

查看:122
本文介绍了如何在异步和同步代码之间切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我知道互联网上堆满了关于这个主题的信息片段,但是它们似乎都没有明确说明你如何整合 async 函数转换成同步代码。



我似乎陷入了摇滚和硬地之间。我必须调用一个只提供 async 函数的API,但这是从一个框架调用的,该框架只提供我可以调用的同步函数。因此,通过我自己的代码传播 async 关键字并不重要,迟早我遇到调用异步<的问题/ code>来自同步代码的方法。



更一般地说,我必须对这些东西做错事。 async / 等待关键字应该简化并行编程。我发现恰恰相反。我已经完成了C,C ++和C#(使用TPL)的并行编程,我不得不说我从未遇到过如此多的问题。



我缺少什么?



亲切的愿望~Patrick



我是什么尝试过:



我很想说我已经尝试了所有的东西,但这个盒子的字符不够。



对于问题的含糊不清很抱歉,但我真的很难找到如何使用asynch的明确解释

Hi,

I know the internet is littered with fragments of information on this subject, but none of them seems to provide a clear explanation of how you incorporate async functions into synchronous code.

I seem to be caught between a rock and a hard place. I have to call an API that provides only an async function, but this is called from a framework that provides only a synchronous function where I can call from. Therefore, it doesn't matter how far I propagate async keywords through my own code, sooner or later I bump into the problem of calling an async method from synchronous code.

More generally, I must be doing something wrong with this stuff. The async/await keywords are supposed to simplify parallel programming. I am finding the exact opposite. I've done parallel programming in C, C++ and now C# (using the TPL), and I have to say that I have never had so many problems as I am having now.

What am I missing?

Kind wishes ~ Patrick

What I have tried:

I'm tempted to say I have tried everything, but that's not enough characters for this box.

Sorry for the vagueness of the question, but I'm really struggling to find clear explanations of how to use asynch

推荐答案

这里是Stephen Cleary写的一篇好文章:

Async / Await - 异步中的最佳实践编程 [ ^ ] < br $> b $ b

但是我害怕他说你不想听的话:

Here is a good article by Stephen Cleary:
Async/Await - Best Practices in Asynchronous Programming[^]

But I'm afraid he says what you don't want to hear:
引用:

当您将同步代码转换为异步代码时,您会发现如果异步代码调用并且被其他异步代码调用 - 它一直向下(或者向上) , 如果你更喜欢)。其他人也注意到异步编程的传播行为,并将其称为传染性或将其与僵尸病毒进行比较。

As you convert synchronous code to asynchronous code, you’ll find that it works best if asynchronous code calls and is called by other asynchronous code—all the way down (or "up," if you prefer). Others have also noticed the spreading behavior of asynchronous programming and have called it "contagious" or compared it to a zombie virus.


这篇关于如何在异步和同步代码之间切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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