A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E) exception was thrown by a method

2

Im creating a method in C# in windows 8 application where I am reading data from XML file and storing it in an object and binding the object value into UI elements.

I am using some asynchronous file read and write methods, Since I am calling this method inside main page constructor I am not able to use await keyword so I am getting the below exception ."A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)"

When I run the application without debugging but when I debug by stepping into the code(using F11) I am getting the desired output. How to get the desired output without debugging

I have pasted the code snippet below

public object mymethod()
{
    var FileshareDetails = Windows.Data.Xml.Dom.XmlDocument.LoadFromFileAsync(file, localsettings);
    var QFileshare = FileshareDetails.GetResults();
    XmlNodeList nodeList = QFileshare.SelectNodes("/FileShareDetails/FileShare");

    foreach (IXmlNode node in nodeList)
    {
        QSFileShare FileShare = new QSFileShare();
        FileShare.FileShareName = node.Attributes[0].InnerText;
        ...
    }
}
windows-8
asked on Stack Overflow Oct 31, 2012 by Mahadev Swamy • edited Oct 31, 2012 by ZombieSheep

2 Answers

4

GetResults is non-blocking, i.e. it will try and get the result from the asynchronously executing method immediately, even if the method hasn't finished running and results aren't there to be got yet.

You have to use the await keyword with methods ending Async. This means that your method mymethod will have to comply with the asynchronous pattern. It's worth reading up on the whole async thing if you're writing Windows 8 apps.

Async is a compiler trick that's been designed to make it easier and to move people towards not blocking the UI thread and making more responsive apps.

Alternatively, if you want to block the thread, you can switch out the GetResults() call with AsTask().Result.

There aren't many good reasons to block, except perhaps for unit test or dirty research/demo code. If you're having to block, you're probably doing something wrong at an architectural level.

answered on Stack Overflow Jun 10, 2013 by Luke Puplett • edited Jun 10, 2013 by Luke Puplett
0

You can put your code inside the onNavigatedTo event

answered on Stack Overflow Oct 31, 2012 by Ibraheem Osama

User contributions licensed under CC BY-SA 3.0