I had asked a question in a different thread about issues with GDI+ in TPL (async/await) and the discussion turned to the question of whether or not there even were any benefits to using TPL for this.
So I'm trying to understand the answer to that here.
The scenario is roughly this:
- A Web API controller/method receives an image upload
- A method which resizes the image and uploads it to azure is called multiple times for various sizes (<10)
- The method returns a Uri for each resized-and-uploaded image
- A response is returned to the Web API client
Note that this will likely run on a single core machine so there is no benefit to be gained by running all the resizes in parallel (to, say, shorten the overall length of the request).
But I'm under the impression that wrapping all the various resizes into a method and running that asynchronously will at least return the Web API thread to the pool, temporarily, to process another request (while a regular thread runs the resizing tasks), and that that is a good thing. The code would look like this:
public Dictionary<ProfilePhotoSize, Uri> ProcessImages(Stream photoStream)
{
var imgUris = new Dictionary<ProfilePhotoSize, Uri>()
{
ProfilePhotoSize.FiveHundredFixedWidth, ResizeAndUpload(ProfilePhotoSize.FiveHundredFixedWidth, photoStream)},
ProfilePhotoSize.Square220, ResizeAndUpload(ProfilePhotoSize.Square220, photoStream)},
ProfilePhotoSize.Square140, ResizeAndUpload(ProfilePhotoSize.Square140, photoStream)},
ProfilePhotoSize.Square80, ResizeAndUpload(ProfilePhotoSize.Square80, photoStream)},
ProfilePhotoSize.Square50, ResizeAndUpload(ProfilePhotoSize.Square50, photoStream)}
};
return imgUris;
}
and...
var photoUris = await Task.Run(() => _photoService.ProcessImages(photoStream);
So the question is - am I off base? Maybe the theory is sound, but it's not implemented quite right (perhaps I need to use ConfigureAwait)?
What's the reality here?