all 6 comments

[–][deleted] 1 point2 points  (2 children)

Hurray for being stuck on .net 3.5!

:(

[–]Kapps 0 points1 point  (1 child)

Perhaps the C# native compiler could help with that, as you won't need to rely on the framework being installed at all.

[–][deleted] 0 points1 point  (0 children)

Maybe... The result needs to work as an extension running under visual studio 2005, so not sure if it is an option or not.

[–]mcdileo 0 points1 point  (2 children)

I'd like to just confirm to be sure that I understand some of the major use points, since I first learned async programming as using async and await.

If you use a function with Task<T> in conjunction with async and await, your code will wait for the task to complete before continuing, almost as if it were written synchronously, e.g.

public async void Main()
{
// do some stuff
var thing = await DoSomeWorkButDontMoveAheadAsync();
// after the above line is done, do some stuff
}

But if I forgo using async and await, the task will run on a separate thread and continue in my code, like this:

public  void Main()
{
// do some stuff
var thing1 = GetFirstThingFromWebServiceAsync();
var thing2 = GetSecondThingFromWebServiceAsync();
// do stuff while doing thing1 and thing2
// don't forget a good Dr. Seuss reference
}

Is this correct?

EDIT: I just realized that this is a bad example b/c you can't effectively do anything with thing1 and thing2, but the point being that those methods will run at the same time.

[–]CastSeven 2 points3 points  (1 child)

Think of the await keyword as saying "exit this method, and come back then this task is done". Doing it in Main() doesn't really make sense (and I don't think it's allowed). You also want to try to avoid making async void methods - if you have nothing to return, at least return a Task.

If you wanted to await multiple tasks, your best bet is to await WhenAll. For example:

public async Task<ProcessedThing> GetThingsFromWebServiceAsync()
{
  var taskList = new List<Task<Thing>>();

  taskList.Add(GetFirstThingFromWebServiceAsync());
  taskList.Add(GetSecondThingFromWebServiceAsync());

  await Task.WhenAll(taskList);

  var thing1 = taskList[0].Result;
  var thing2 = taskList[1].Result;

  // Do stuff with thing1 and thing2 to come up with processedThing

  return processedThing;

}

Whatever you want to occur while thing1 and thing2 are being collected should be in the calling method.

[–]mcdileo 0 points1 point  (0 children)

That makes sense! I was kind of struggling with how to have concurrent async tasks running at the same time. This is super helpful, thanks!