当前位置: 代码迷 >> Windows Mobile >> WP8/Win8异步读写资料
  详细解决方案

WP8/Win8异步读写资料

热度:49   发布时间:2016-04-25 07:21:38.0
WP8/Win8异步读写文件
本帖最后由 hnsdwhl 于 2013-02-21 09:17:23 编辑
在WP8/Win8提供的API里面,对于文件的读写都是采用异步的方式。现在我需要等待文件读写完成,并将读取的文件内容保存到变量中,以供后面使用。但是还未等我将内容保存到变量中,程序就往下面执行了,这时变量还没有值,请问应该要怎样做,让变量赋值完成再往下走。相关的代码如下。

public static class Test
{
    private static ushort[] testArray = new ushort[0xffff];

    static Test()
    {
        LoadFile();
        //下面需要使用testArray变量,但是此时变量还没有值
     }

    public async static void LoadFile()
    {
        StorageFolder packageFolder = Package.Current.InstalledLocation;
        StorageFile packagedFile = await packageFolder.GetFileAsync(@"test.bin");
        using (Stream stream = await packagedFile.OpenStreamForReadAsync())
        {
            using (BinaryReader reader = new BinaryReader(stream))
            {
                for (int i = 0; i < 0xffff; i++)
                {
                    testArray[i] = reader.ReadUInt16();
                }
            }
        }
    }
}

------解决方案--------------------
await就会等待呀.
你不是已经加了await了吗
------解决方案--------------------
你调用LoadFile();的时候不await,当然不等你了.
------解决方案--------------------
  相关解决方案