Thursday, 8 August 2013

Async method sometimes never ends

Async method sometimes never ends

I am taking images and processing them. Then I am trying to save
bitmaps(these images) in a file.
For example
I take "desert" and "flower" images and I process them 4 times each one
asynchronously.
Then I call(await) 8 times saveAsync() method to save them.
What happens is desert_modified and flower_modified, flower_modified(1),
flower_modified(2), flower_modified(3) are saved.
There are no images desert_modified(1), desert_modified(2),
desert_modified(3).
The methods that are going to save them never finishes. Any suggestion?
async private Task saveAsync(Bitmap bitmap, String path, String fileName,
int requestNo)
{
int temp = 1;
String pth = path;
String fileName1 = fileName;
try
{
await Task.Run(() =>
{
if (File.Exists(pth))
{
String[] array = fileName1.Split('.');
String modifiedFileName = array[0] + "_modified" + "("
+ temp + ")." + array[1];
pth = fullPath.Replace(fileName1, modifiedFileName);
while (File.Exists(pth))
{
temp++;
//array = fileName.Split('.');
modifiedFileName = array[0] + "_modified" + "(" +
temp + ")." + array[1];
pth = fullPath.Replace(fileName1, modifiedFileName);
}
bitmap.Save(@pth);
}
else
{
bitmap.Save(@pth);
}
bitmap.Dispose();
});
}
catch (Exception ex)
{
outputTextBox2.Text += ex.InnerException.Message;
}
}

No comments:

Post a Comment