我有以下场景:
当输入命令时(为了测试,它是一个控制台应用程序,当它准备好时,我希望它是一个 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 尝试了许多工厂方法,但我找不到合适的方法!请帮忙!