简体   繁体   中英

async method within non-async loop

I am using a loop to place images in my canvas, there is a method that needs to check if each looped images have an attribute that is stored in a JSON file.

the checkImage method works fine outside the loop, but when I place it in the loop it gives this error:

"Access denied, file is used by another process"

Since the method gets called for every image created by the loop I guess that I need to close the JSON file after its serialized or something, but how?

The loop:

 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 method:

 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);

    }

Return the Task instead of void. And await for it complete in loop like this.

  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;
}

In this answer you can find good explanation difference between returning void and Task. In you case you should change return type to Task and await it in your loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM