2016-03-25 64 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を返すことの間に良い説明の違いがあります。あなたの場合、戻り値の型をTaskに変更し、ループ内で待つ必要があります