Theo
Theo

Reputation: 588

XDocument.Load() causes System.InvalidOperationException XAML C#

To parse some XML from a file that the user specifies, I have the following C# code:

private async void AskUserForFile()
{
    var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
    FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
    FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
    FilePicker.FileTypeFilter.Add(".pcs");
    FilePicker.FileTypeFilter.Add(".pcp");

    XMLFile = await FilePicker.PickSingleFileAsync();
    ParseXML();
}

private void ParseXML()
{
    XDocument Files = XDocument.Load(XMLFile.Path);
}

Which returns the following error:

An exception of type 'System.InvalidOperationException' occurred in System.IO.FileSystem.dll but was not handled in user code Additional information: Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run.

Can someone please help me figure out why this is happening and how to fix it? Thanks in advance.

Note: I am programming for Universal Windows.

Upvotes: 0

Views: 439

Answers (2)

Paul
Paul

Reputation: 176

Something like the code below should get you past the error, but it kind of defeats the purpose of the async method.

I can't give you a specific example on your project but maybe the 'askuserforfile' operation should trigger an event that builds your xml to support the whole 'non-blocking-thread' concept for modern windows app development. Or hook into 'onpropertynofitychange' on the model that is asking the user for a file to build the xml after the async operation has completed.

Note: in the snippet below, remove the Task.Wait() I inserted for debugging on my end ... unless you need the xml built before the next operation can proceed.

public class Ans34558541
{
    public StorageFile XMLFile { get; private set; }
    public XDocument Files { get; private set; }

    private async void AskUserForFile()
    {
        var FilePicker = new Windows.Storage.Pickers.FileOpenPicker();
        FilePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
        FilePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.ComputerFolder;
        FilePicker.FileTypeFilter.Add(".pcs");
        FilePicker.FileTypeFilter.Add(".pcp");

        XMLFile = await FilePicker.PickSingleFileAsync();
        Action Act = new Action(ParseXML);
        Task Tsk = new Task(Act);
        Tsk.Start();
        Tsk.Wait();
    }

    private async void ParseXML()
    {
        String xml = await FileIO.ReadTextAsync(XMLFile);
        File = XDocument.Parse(xml);
    }
}

Upvotes: 1

Kevin Green
Kevin Green

Reputation: 1205

It is giving you the answer in the error. "Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run."

You should not be performing synchronous loading of a file on the UI thread. Try wrapping your call in a Task.Run. You can use the Lambda notation to make it easy to do.

https://msdn.microsoft.com/en-us/library/hh195051(v=vs.110).aspx

Upvotes: 0

Related Questions