그래서 약간의 문제가 있습니다. 그래서 같은 방법의 집합이 있습니다FileIO.AppendTextAsync 루프에서 사용할 때 임의의 예외가 throw 됨
StorageFile file;
public void WriteStuff() //This implements an interface, so can't be changed to async with a task returned
{
Write();
}
async void Write()
{
await FileIO.AppendTextAsync(file, DateTime.Now + " testing!"+Environment.NewLine);
}
async void Create()
{
file=await ApplicationData.Current.LocalFolder.CreateFileAsync("test.txt", CreationCollisionOptions.OpenIfExists);
}
다음 코드는 사실과 같이 그것을 사용 :
var x=new Foo();
for(int i=0;i<1000;i++)
{
x.WriteStuff(); //can't use async/await from here. This is in a portable class library
}
내가 Test
를 호출 할 때마다
AppendTextAsync
을 여러 번 비동기 적으로 호출 할 때만 나타나는 것으로 보입니다. 나는 비동기적인 API가 될 것이라고 상상했을 것이다 .. 글쎄, 스레드 안전하지만 분명히 아닙니다. 사실입니까?
또한 내 전화 코드를 수정하지 않고 어떻게 해결할 수 있습니까? 그래서,
void Write()
{
FileIO.AppendTextAsync(
file, DateTime.Now + " testing!"+Environment.NewLine).Wait();
}
void Create()
{
file = ApplicationData.Current.LocalFolder.CreateFileAsync(
"test.txt", CreationCollisionOptions.OpenIfExists).Result;
}
을하지만 이것은 비동기 코드의 전체 목적을 격파 : 당신이 정말이 작업을 수행해야하는 경우
죄송 합니다만, '비동기 작업'을 사용해야합니다. – SLaks
Asynchrony는 스레딩과 아무 관련이 없습니다. 비동기 API는 거의 스레드로부터 안전하지 않습니다. – SLaks