This might be a very stupid question, but I have the following lines of coding that convert RAW images to BitmapImages:
public async void CreateImageThumbnails(string imagePath, int imgId)
{
await Task.Run(() => controlCollection.Where(x => x.ImageId == imgId).FirstOrDefault().ImageSource = ThumbnailCreator.CreateThumbnail(imagePath));
}
which calls this method CreateThumbnail()
public static BitmapImage CreateThumbnail(string imagePath)
{
var bitmap = new BitmapImage();
using (var stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
{
bitmap.BeginInit();
bitmap.DecodePixelWidth = 283;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.StreamSource = stream;
bitmap.EndInit();
}
bitmap.Freeze();
GC.WaitForPendingFinalizers();
GC.Collect();
return bitmap;
}
When using async Void
instead of async Task
in my CreateImageThumbnails
method, my application processes the images(29 of them) about 11 seconds faster than async Task
. Why would this be?
async void
async task
The memory usage is much more using void
, but the operation is completed much quicker. I have little knowledge of threading, this is why I am asking this question. Can someone please explain why this is happening?
Also I have done some research on on when and when not to use async void
, but I could not find an answer to my question. (I might just not have searched very well).
Thank you.