如何编写C#5异步? [英] How to write C# 5 async?
问题描述
我有以下情形:结果
当命令被输入(用于测试,这是一个控制台应用程序,当它准备好了,我希望这将是一个WebService)我执行一些code,并且需要进一步的用户输入的时候,我回到命令间$ P马上$ PTER。当新的输入给定,我要处理,从我离开那里重新开始。这听起来这么像C#5异步等待模式,我决定试一试。
我在想这个问题:
I have the following scenario:
When a command is inputted (for test, it's a console application, when it's ready, I hope it will be a WebService) I execute some code, and when further user input is needed, I return to the command interpreter immediately. When the new input is given, I want processing to resume from where I left it. That sounds so much like c#5 async-await pattern, that I decided to give it a try.
I was thinking about this:
public void CommandParser()
{
while(true)
{
string s = Console.ReadLine();
if (s == "do_something")
Execute();
else if (s == "give_parameters")
SetParameters();
//...
}
}
MySettings input;
public async void Execute()
{
//do stuff here
MyResult result = null
if (/*input needed*/){
input = new MySetting();
result = await input.Calculate();
}
else { /* fill result synchronously*/}
//do something with result here
}
public void SetParameters()
{
if (input!=null)
input.UseThis("something"); //now it can return from await
}
现在我的问题是,如何写MySettings.Calculate和MySettings.UseThis?如何从第一返回任务,以及如何从第二信号readyness?我试过许多工厂方法的任务,但我不能找到合适的人!请帮助!
Now my question is, how to write MySettings.Calculate and MySettings.UseThis? How to return a Task from the first and how to signal readyness from the second? I've tried with many factory methods for Task, but I can't find the right one! Please help!
推荐答案
一种选择是使用的 TaskCompletionSource< T>
。这将建立一个任务你,你可以叫的setResult
或 SetException
的来源,这将标志着任务适当。
One option is to use TaskCompletionSource<T>
. That will build a task for you, and you can call SetResult
or SetException
on the source, which will signal the task appropriately.
这是我用什么来<一个href=\"http://msmvps.com/blogs/jon_skeet/archive/2011/05/15/eduasync-part-4-filling-in-asynctaskmethodbuilder-lt-t-gt.aspx\"相对=nofollow>实施 AsyncTaskMethodBuilder&LT; T&GT;
为Eduasync - 所以你可以看一下的例子
That's what I've used to implement AsyncTaskMethodBuilder<T>
for Eduasync - so you can look at that for an example.
您会需要或者建立 TaskCompletionSource
事前或执行一些其他的协调,以便 input.Calculate
和 UseThis
都知道同一个对象 - 但计算
将只返回 completionSource.Task
和 UseThis
会叫 completionSource.SetResult
。
You'd need to either set up the TaskCompletionSource
beforehand or perform some other coordination so that input.Calculate
and UseThis
both know about the same object - but then Calculate
would just return completionSource.Task
, and UseThis
would call completionSource.SetResult
.
记住,当你调用的setResult
,异步方法会继续下去在不同的线程池线程,如果你使用一个控制台应用程序(或Web服务) - 所以你无疑是想建立一个的不同的的 TaskCompletionSource
的主循环再使用,为下一轮,因为它是。
Bear in mind that when you call SetResult
, the async method will keep going on a different thread-pool thread if you're using a console app (or web service) - so you'd no doubt want to create a different TaskCompletionSource
for the main loop to then use for the next round, as it were.
这篇关于如何编写C#5异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!