3

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!

2
  • Its not directly relavant to your question, but how do you have c# 5? Commented May 26, 2011 at 21:35
  • 1
    i'm trying to use the async ctp: microsoft.com/downloads/en/… Commented May 26, 2011 at 21:43

1 Answer 1

6

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.

That's what I've used to implement AsyncTaskMethodBuilder<T> for Eduasync - so you can look at that for an example.

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.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks, it looks very promising. I'll try implement my actual code, then check back for the accept.
Quick question though, I have installed the CTP, but await doesn't seem to compile. What am I doing wrong?
@TDaver: What errors are you getting? If the CTP installed cleanly, the compiler should have been changed appropriately...
I've since then figured out that my problem is that I have some post-sp1 update installed, and it blocks the async ctp. My problem is I don't know what update is that, so right now I'm uninstalling every .net and vs update I can find....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.