I have one asynchronous method:
public async Task<BitmapSource> GetBitmapAsync(double[] pixels);
Let's say I also have this class:
public class PixelData
{
public double[] Pixels { get; }
}
I now want to create a convenience method producing a BitmapSource
output, using the asynchronous method above to do the work. I can come up with at least three approaches to do this, but it is not immediately obvious to me which one I should choose from an efficiency and reliability point of view.
Can someone advice; what are the advantages and drawbacks of each one of the following approaches?
Option A Create a synchronous method that returns the Result
of the Task
:
public BitmapSource GetBitmap(PixelData pixelData)
{
return GetBitmapAsync(pixelData.Pixels).Result;
}
Option B Create a synchronous (or is it asynchronous?) method that returns Task<BitmapSource>
:
public Task<BitmapSource> GetBitmap(PixelData pixelData)
{
return GetBitmapAsync(pixelData.Pixels);
}
Option C Create an asynchronous method that explicitly uses await
:
public async Task<BitmapSource> GetBitmapAsync(PixelData pixelData)
{
return await GetBitmapAsync(pixelData.Pixels);
}
Task<BitmapSource>
, is that irrelevant with respect to (a)synchronicity? – Aperientawait
- though I haven't got VS2012 in front of me to double check. – Novenaawait
for Option B. It is thus asynchronous, even if it is not evident from the method signature. – Aperientawait
aTask
regardless of whether it's returned from anasync
or non-async
method. – Demijohn