c# - 为通用应用程序读取/写入异步文件

标签 c# xml asynchronous win-universal-app uwp

我正在尝试在 C# 中为通用应用程序读取/写入异步文件。 当我第一次写入和读取文件时,它可以工作......但是当我立即重试时,有两个错误:1. UnauthorizedAccess 2. OPLOCK 处理已关闭

看来方法还没有完成,所以数据还没有释放

(在我的框架中有一个按钮,它将新成员添加到列表中,然后该列表应在 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();
        }
    }

那么我能做什么,为什么这么难??(普通 I/O 很容易).-.

最佳答案

正如我在关于异步最佳实践的 MSDN 文章中所述,you should avoid async void :

public async Task WriteImmoListAsync();
public async Task ReadImmoListAsync();

一旦您的方法正确异步任务,那么您就可以等待它们:

await WriteImmoListAsync();   
await ReadImmoListAsync();

await WriteImmoListAsync();
await ReadImmoListAsync();   

关于c# - 为通用应用程序读取/写入异步文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37593580/

相关文章:

node.js - 为什么 Promiseify 会导致循环花费更长的时间?

c# - 动态创建 <Type> 的对象

c# - 从另一个集合更新 ObservableCollection

python - Python 3.4 和阅读这个简单的 XML 站点有什么关系?

xml - 如何使用 XQuery 更新 XML 变量中的属性值?

html - 适用于结构化数据的文本友好文件格式

python - Celery:何时何地使用运行@task 的.delay() 的返回值来更新数据库?

asynchronous - 集成同步和异步库

c# - '<', 十六进制值 0x3C, 是一个无效的属性字符

c# - Sitecore 子项目排序问题