2016-03-25 62 views
0

我正在使用循環將圖像置於畫布中,有一種方法需要檢查每個循環圖像是否具有存儲在JSON文件中的屬性。非異步循環內的異步方法

的checkImage方法外循環工作正常,但是當我把它放在循環就給出了這樣的錯誤:由於該方法被調用由循環創建的每個圖像我想我需要

"Access denied, file is used by another process"

在序列化之後關閉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); 

    } 
+1

什麼循環?另外,'async void'是一個着名的壞主意。 – David

+0

增加了循環,沒想到這是必要的。爲什麼這是一個壞主意?有可能是一個更好的方法來做到這一點,但不能想到1. – Yoshi

+1

因爲'void'不能等待。請注意,在循環中,您並未等待該方法。如果對該方法的多次調用同時嘗試寫入同一個文件,那麼文件系統將拒絕該文件。 – David

回答

4

返回任務,而不是無效的。等待它像這樣完成循環。

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

似乎現在工作,將進一步測試。乾杯! – Yoshi

1

this answer你可以在返回void和Task之間找到很好的解釋區別。在你的情況下,你應該改變返回類型爲任務,並在你的循環中等待它