非异步循环中的异步方法

时间:2016-03-25 14:31:28

标签: c# asynchronous win-universal-app

我使用循环将图像放在画布中,有一种方法需要检查每个循环图像是否具有存储在JSON文件中的属性。

checkImage方法在循环外工作正常,但当我将它放在循环中时会出现此错误:

  

"访问被拒绝,文件由另一个进程使用"

由于为循环创建的每个图像调用了该方法,我猜我需要在序列化之后关闭JSON文件,但是如何?

循环:

 while (i < blauw)
                {
                    checkImage(i); //checks if it has the attribute or not
                    ImageBrush brush = new ImageBrush();
                    Button Btn = new Button();
                    Uri uri = new Uri("ms-appx://Blijfie/images/" + selected + "/" + blauwpics[i]);
                    BitmapImage imgSource = new BitmapImage(uri);
                    brush.ImageSource = imgSource;
                    Btn.Background = brush;
                    Btn.Width = 200;
                    Btn.Height = 200;

                    if (i >= 5 && i < 10)
                    {
                        Canvas.SetTop(Btn, marginTop);
                        Canvas.SetLeft(Btn, marginLeft2);
                        marginLeft2 = marginLeft2 + 250;
                    }
                    else if (i < 6)
                    {
                        Canvas.SetLeft(Btn, marginLeft);
                        marginLeft = marginLeft + 250;

                    }
                    else if (i >= 10)
                    {
                        Canvas.SetTop(Btn, marginTop2);
                        Canvas.SetLeft(Btn, marginLeft3);
                        marginLeft3 = marginLeft3 + 250;
                    }
                    main.Children.Add(Btn);
                    i++;

                }

CheckImage方法:

 private async void checkImage(int id) {
                    var foldertest = ApplicationData.Current.TemporaryFolder;
                    var files = await foldertest.GetFilesAsync();
                    var desiredFile = files.FirstOrDefault(x => x.Name == "Dierendb.json");
                    var textContent = await FileIO.ReadTextAsync(desiredFile);

                    var result = JsonConvert.DeserializeObject<List<Dier>>(textContent);

                    id++;

                    if (result[id].PathDier.Equals(""))
                    {
                        //do this
                    }
                    else
                    {
                        //do that
                    }


                    string dierenlijst = JsonConvert.SerializeObject(result, Formatting.Indented);
                    await Windows.Storage.FileIO.WriteTextAsync(desiredFile, dierenlijst, Windows.Storage.Streams.UnicodeEncoding.Utf8);

    }

2 个答案:

答案 0 :(得分:4)

返回Task而不是void。并等待它像这样循环完成。

  while (i < blauw)
                {
                    var bool= await checkImage(i); //checks if it has the attribute or not
                    ImageBrush brush = new ImageBrush();
                    Button Btn = new Button();
                    Uri uri = new Uri("ms-appx://Blijfie/images/" + selected + "/" + blauwpics[i]);
                    BitmapImage imgSource = new BitmapImage(uri);
                    brush.ImageSource = imgSource;
                    Btn.Background = brush;
                    Btn.Width = 200;
                    Btn.Height = 200;

                    if (i >= 5 && i < 10)
                    {
                        Canvas.SetTop(Btn, marginTop);
                        Canvas.SetLeft(Btn, marginLeft2);
                        marginLeft2 = marginLeft2 + 250;
                    }
                    else if (i < 6)
                    {
                        Canvas.SetLeft(Btn, marginLeft);
                        marginLeft = marginLeft + 250;

                    }
                    else if (i >= 10)
                    {
                        Canvas.SetTop(Btn, marginTop2);
                        Canvas.SetLeft(Btn, marginLeft3);
                        marginLeft3 = marginLeft3 + 250;
                    }
                    main.Children.Add(Btn);
                    i++;

               }

Private Task<bool> CheckImage(int i)
{
Yourcode
Return true;
}

答案 1 :(得分:1)

this answer中,您可以找到返回void和Task之间的良好解释差异。在这种情况下,您应该将返回类型更改为Task并在循环中等待它