简体   繁体   English

C#async,等待没有任务

[英]C# async, await without tasks

By creating one or more awaiters and awaitables, is it possible to build coroutines in C#? 通过创建一个或多个awaiters和awaitables,是否可以在C#中构建协同程序?

Ideally I would like to be able to write something like: 理想情况下,我希望能够写出如下内容:

void async Click() {
  var mouse_position = await left_mouse_click();
  await shoot_projectile();
}

and then obtaining from the Click method something that I can either await inside another async method or that I can explicitly iterate with some MoveNext/GetResult method. 然后从Click方法中获取一些我可以在另一个异步方法中等待的东西,或者我可以用一些MoveNext / GetResult方法显式迭代。

The end goal is that I need to integrate C# async/await with F# monadic coroutines for a game development framework ( http://casanova.codeplex.com ). 最终目标是我需要将C#async / await与F#monadic coroutines集成到一个游戏开发框架( http://casanova.codeplex.com )。

By creating one or more awaiters and awaitables, is it possible to build coroutines in C#? 通过创建一个或多个awaiters和awaitables,是否可以在C#中构建协同程序?

Sure! 当然! I have a blog post that uses TaskScheduler to build simple coroutines, and Jon Skeet has a blog post that uses custom awaitables to build simple coroutines. 我有一篇博客文章 ,使用TaskScheduler构建简单的协同程序,Jon Skeet有一篇博文 ,使用自定义等待来构建简单的协同程序。

But looking at the rest of your question and comments, I'm not sure if coroutines is what you really want. 但是看看你的其他问题和评论,我不确定协程是否是你真正想要的。 If you want to "iterate" over awaitables, your best bet is Reactive Extensions (which interoperates well with async ). 如果你想要“迭代”等待,那么最好的选择是Reactive Extensions (与async互操作)。 There is no C# language feature for an "async yield". “async yield”没有C#语言功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM