嵌套Stream调用中的System.UnauthorizedAccessException

时间:2017-02-07 20:41:16

标签: c# xml ienumerable xelement

我在此功能中获得System.UnauthorizedAccessException

public static async Task<IEnumerable<XElement>> XMLDisplaySignals()
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            StorageFile file = await storageFolder.GetFileAsync("sFile.xml");
            XDocument doc;
            try
            {
                using (var stream = await file.OpenStreamForReadAsync())
                {
                    doc = XDocument.Load(stream);
                    IEnumerable<XElement> signals = doc.Descendants("signals");
                    return signals;
                }
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.StackTrace);
                return null;
            }
        }

for循环中调用5-10次而没有Sleep。我的for循环看起来像这样:

for (int j = 0; j < iItems[0].Count; j++)
            {
                IEnumerable<XElement> signals = await SampleDataSource.XMLDisplaySignals();
                if (signals != null && signals.Descendants("signal").Any())
                {
                    if(!signals.Descendants("signal").Any(x => x.Element("title").Value == iItems[0][j]))
                    {
                        Debug.WriteLine("Adding: " + iItems[0][j]);
                        await SampleDataSource.XMLAddXElement(new XElement("signal", new XElement("title", iItems[0][j]), new XElement("body", iItems[1][j])));
                    }
                    else
                    {
                        Debug.WriteLine("Already in");
                    }
                }
                else
                {
                    //handle the bug hardcore
                    await SampleDataSource.XMLAddXElement(new XElement("signal", new XElement("title", iItems[0][j]), new XElement("body", iItems[1][j])));
                }
        }

我相信这种情况正在发生,因为我正在处理xml写入和读取非常快。除了添加Sleeps之外,还有解决方法吗? XMLDisplaySignals看起来像这样,处理和xml写

public static async Task XMLAddXElement(XElement element)
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
            StorageFile file = await storageFolder.GetFileAsync("sFile.xml");

            XDocument doc;

            try
            {
                using (var stream = await file.OpenStreamForReadAsync())
                {
                    doc = XDocument.Load(stream);
                    var signals = doc.Descendants("signals").Single();
                    signals.Add(element);
                    using (var stream2 = await file.OpenStreamForWriteAsync())
                    {
                        stream2.SetLength(0);
                        doc.Save(stream2);
                    }
                }
            }
            catch(Exception ex)
            {
                //Debug.WriteLine(ex.StackTrace);
            }
        }

P.S。如果我在for循环中添加300MS的睡眠,问题就解决了。但是我们都知道睡眠不是解决方案。

1 个答案:

答案 0 :(得分:0)

你是对的。睡眠不是解决方案,因为您无法确定操作需要多长时间。

您需要在使用资源之前锁定资源(即在OpenStreamForReadAsync方法中调用XMLDisplaySignals之前),以便在资源从上一次运行中释放之前不会尝试操作。

This page说明了如何将AsyncSemaphore用于此目的。