【发布时间】:2011-09-02 23:12:42
【问题描述】:
我有以下情况:
当输入命令时(为了测试,它是一个控制台应用程序,当它准备好时,我希望它是一个 WebService)我执行一些代码,当需要进一步的用户输入时,我立即返回命令解释器。当给出新的输入时,我希望处理从我离开的地方恢复。这听起来很像 c#5 async-await 模式,所以我决定试一试。
我在想这个:
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?如何从第一个返回任务以及如何从第二个发出准备就绪信号?我已经为 Task 尝试了许多工厂方法,但我找不到合适的方法!请帮忙!
【问题讨论】:
-
它与您的问题没有直接关系,但是您如何拥有 c# 5?
-
我正在尝试使用异步 ctp:microsoft.com/downloads/en/…
标签: c# asynchronous async-await c#-5.0