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
}
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!