c# - Windows 8 Metro App 仅在读取文件时在调试器中启动

标签 c# windows-8 xna async-await

我正在使用 XNA 和 Monogame 编写 Windows 8 Metro 应用程序。 我正在使用此代码读取文件:

File reading:

private void getDoc(String filename)
    {
        readstate = 0;

        try
        {
            // get the file
            //StorageFile myStorageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);
            String path = @"birdy2 - win8\XML\XMLFiles\" + filename + ".xml";
            StorageFile myStorageFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(path);


            var readThis = await FileIO.ReadLinesAsync(myStorageFile);
            String txt = "";
            foreach (var line in readThis)
            {
                txt += line.ToString();
            }
            if (txt.Length > 0)
            {
                doc = XDocument.Parse(txt);
            }
            readstate = 1;

        }
        catch (FileNotFoundException ex)
        {
            doc = null;
            readstate = 2;
        }

    }

然后我调用这个方法:

        XDocument gameinfo = null;
        try
        {
            getDoc("gameinfo.xml");
            while (readstate == 0) { }
            gameinfo = doc;
        }
        catch { gameinfo = null; }

问题: 如果我调试应用程序,一切正常。 如果我释放该应用程序,然后我直接从 Win8 启动屏幕启动它,该应用程序不会启动。 我发现

while (readstate == 0) { }

readstate 在释放状态下永远不会与 0 有任何不同。但不知道为什么。

我对每个答案都很满意。 谢谢!



感谢 Nahel 的快速回答! 我重写了代码,但它仍然对我不起作用,与以前的行为相同。

private async Task<int> getDoc(String filename)
    {
        try
        {
            // get the file
            StorageFile myStorageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(filename);                         
            var readThis = await FileIO.ReadLinesAsync(myStorageFile);

            String txt = "";
            foreach (var line in readThis)
            {
                txt += line.ToString();
            }
            if (txt.Length > 0)
            {
                doc = XDocument.Parse(txt);
            }
            return 1;

        }
        catch (FileNotFoundException ex)
        {
            doc = null;
            return 2;
        }

    }

加载:

        XDocument gameinfo = null;
        try
        {
            Task<int> tsk = getDoc("gameinfo.xml");               
            int result = tsk.Result;
            if (result == 1)
            {
                gameinfo = doc;
            }
            else
            {
                gameinfo = null;
            }

        }
        catch { gameinfo = null; }

最佳答案

您正在使用具有一些async 操作但未将其标记为async 的方法。

getDoc 方法标记为 async 并在您调用它的地方等待它。

不使用 readstate 变量,而是从 getDoc 方法返回一个 int 并在主调用中使用该对象。或者更好的是,返回您要使用的文档。

下面是 async/await 调用如何工作的图片:

enter image description here

如果您的方法中有一个 await,一旦代码到达它;它会返回给它的调用者。在那之后无限循环对你没有帮助。

Source .

更新:

根据您的新答案,您在 getDoc 调用中缺少 await

替换:

Task<int> tsk = getDoc("gameinfo.xml");

与:

Task<int> tsk = await getDoc("gameinfo.xml");

关于c# - Windows 8 Metro App 仅在读取文件时在调试器中启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24593881/

相关文章:

c# - 了解 WPF 派生 WIndow 类

c# - 内部 .NET Framework 数据提供程序错误 1025

Windows 8 支持 d3d8.dll?

c# - 在 OnSuspending 方法期间保存数据

c# - 没有瓷砖 XNA 的 2D 游戏中的碰撞检测

c# - 将 XNA 3.1 转换为 XNA 4.0。 StorageContainer.TileLocation

c# - 使用 ILGenerator.Emit 在另一个具有 out 参数的程序集中调用方法

c# - 无法在 WPF 项目中加载文件或程序集

windows-8 - 在 Windows 8 中的 TextBlock 中打印换行符

c# - XNA TCP Socket多次发送丢包