[英]Reading/Writing Async Files for Universal App
im trying to Reading/Writing Async Files for an Universal App in c#. 我试图在c#中读取/写入通用应用程序的异步文件。 When i write and read a file for first time, it works... But when i retry it immeadiatly, there are two Errors: 1. UnauthorizedAccess 2. Handle with the OPLOCK has been closed
当我第一次读写文件时,它可以工作...但是当我立即重试它时,会出现两个错误:1. UnauthorizedAccess 2. OPLOCK的句柄已关闭
It seems that the methods arent finished yet and so the data is not free 看来方法尚未完成,因此数据不是免费的
(in my frame is a button which adds a new member to a List, then the list shall serialized in an XML data. When i reNavigate to that page, that XML sheet shall be deserialized back to that List, because the Content shall be displayed) (在我的框架中是一个向列表添加新成员的按钮,然后该列表应以XML数据序列化。当我重新导航至该页面时,该XML工作表应反序列化回该列表,因为将显示内容)
List<Immobilie> immoListe = new List<Immobilie>();
private const string FileName_ImmoObjects = "ImmoObjects.xml";
StorageFolder sFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
IStorageFile latestImmoListFile;
public Startmenue()
{
this.InitializeComponent();
immoListe.Add(new Immobilie()); // for testing creating an XML first
immoListe[0].adresse = "Foo1";
immoListe.Add(new Immobilie());
immoListe[1].adresse = "Foo2";
WriteImmoListAsync();
ReadImmoListAsync(); // These two steps working
WriteImmoListAsync(); // everything more causes error
ReadImmoListAsync();
}
public async void WriteImmoListAsync()
{
try
{
IStorageFolder folder = await sFolder.CreateFolderAsync("Saves", CreationCollisionOption.OpenIfExists);
latestImmoListFile = await folder.CreateFileAsync(FileName_ImmoObjects, CreationCollisionOption.ReplaceExisting);
using (IRandomAccessStream stream = await latestImmoListFile.OpenAsync(FileAccessMode.ReadWrite))
using (Stream outputStream = stream.AsStreamForWrite())
{
DataContractSerializer serializer = new DataContractSerializer(typeof(List<Immobilie>));
serializer.WriteObject(outputStream, immoListe);
}
}
catch (Exception e)
{
var d = new MessageDialog(e.ToString());
await d.ShowAsync();
}
}
public async void ReadImmoListAsync()
{
int i = 0;
try
{
IStorageFolder folder = await sFolder.GetFolderAsync("Saves");
i = 1;
latestImmoListFile = await folder.GetFileAsync(FileName_ImmoObjects);
i = 2;
using (IRandomAccessStream stream = await latestImmoListFile.OpenAsync(FileAccessMode.Read))
{
i = 3;
using (Stream inputStream = stream.AsStreamForRead())
{
i = 4;
DataContractSerializer deserializer = new DataContractSerializer(typeof(List<Immobilie>));
i = 5;
immoListe = (List<Immobilie>)deserializer.ReadObject(inputStream);
}
}
}
catch (Exception e)
{
var d = new MessageDialog("Fehler I = " + i + "\n" + e.ToString());
await d.ShowAsync();
}
}
So what can i do and why is it so difficult??(normal I/O is easy-peasy).-. 那么我该怎么办,为什么这么困难?(普通的I / O很容易实现).-
As I describe in my MSDN article on async
best practices, you should avoid async void
: 正如我在有关
async
最佳实践的MSDN文章中所描述的那样, 您应该避免使用async void
:
public async Task WriteImmoListAsync();
public async Task ReadImmoListAsync();
Once your methods are properly async Task
, then you can await
them: 一旦您的方法正确地
async Task
,那么您可以await
它们:
await WriteImmoListAsync();
await ReadImmoListAsync();
await WriteImmoListAsync();
await ReadImmoListAsync();
You can't start the methods again until you wait for them to complete. 您必须等到方法完成才能重新启动方法。 What that above code is trying to do is to write to a file, but while that's processing, it tries to open the file and write to it while the first method call hasn't completed.
上面的代码试图执行的操作是写入文件,但是在处理过程中,它会尝试打开文件并在第一个方法调用尚未完成时写入文件。 You need to wait for those method calls to finish before running them again - using the
await
keyword would be helpful here 您需要等待这些方法调用完成后才能再次运行它们-在此处使用
await
关键字会有所帮助
It might be that the process writing/reading the file are still attached to the file. 可能是写入/读取文件的过程仍附加在文件上。 You might want to take a look at this pattern for async file read/write from Microsoft:
您可能想看看这种模式来从Microsoft读取/写入异步文件:
https://msdn.microsoft.com/en-ca/library/mt674879.aspx https://msdn.microsoft.com/en-ca/library/mt674879.aspx
Also, note that if the read and write are done from differents process, you're going to have to use a mutex. 另外,请注意,如果读取和写入是通过差异过程完成的,则必须使用互斥锁。 Here's a great explanation on how it works:
这是有关其工作原理的很好的解释:
What is a good pattern for using a Global Mutex in C#? 在C#中使用Global Mutex的良好模式是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.