uwp异步方法跳过代码行

时间:2016-03-22 11:34:42

标签: c# asynchronous methods async-await win-universal-app

我有一个带有void返回类型的异步方法。这是

public static async void LoadPlaylists()
{
    if(playlistitems.Count==0)
    {
        var playlists = await ApplicationData.Current.LocalFolder.GetFilesAsync();
        var c = playlists.Count;
        foreach (var playlist in playlists)
        {
            var p = await Playlist.LoadAsync(playlist);
            var image = new BitmapImage();
            if (p.Files.Count == 0)
            {
                image.UriSource = new Uri("ms-appx:///Assets/Wide310x150Logo.scale-200.png");
            }
            else
            {
                image = await Thumbnail(p.Files[0]);
            }
            playlistitems.Add
            (
                new PlaylistItem
                {
                    playlist = p,
                    PlaylistName = playlist.DisplayName,
                    NumOfVid = p.Files.Count.ToString(),
                    Thumbnail = image
                }
            );
        }
    }
}

这是一个公共静态方法,所以我可以在任何地方使用它,我在一个页面上使用它来加载一些数据,它工作正常,它完成了这个方法,然后继续下一个代码行。如下图所示

private void l1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //some more code
    LoadPlaylists();
    //some more code
}

但是当我在另一个事件处理程序的另一个页面上使用它时,它不是异步方法,它只运行第一行,然后它跳过整个方法并向前移动。我肯定知道它正在跳过这些线路,bcz我用断点检查,我知道它是跳过因为它是异步但我不想要那个,我只是想让它完成整个方法然后继续前进。我在下一个代码行上没有遇到任何问题。我再次粘贴下面的代码,向您展示其跳过的评论。

public static async void LoadPlaylists()
{
    if(playlistitems.Count==0)
    {
        //it runs till here, when compiler goes to line below
        //it skips whole methods and exits it.
        var playlists = await ApplicationData.Current.LocalFolder.GetFilesAsync();
        var c = playlists.Count;
        foreach (var playlist in playlists)
        {
            var p = await Playlist.LoadAsync(playlist);
            var image = new BitmapImage();
            if (p.Files.Count == 0)
            {
                image.UriSource = new Uri("ms-appx:///Assets/Wide310x150Logo.scale-200.png");
            }
            else
            {
                image = await Thumbnail(p.Files[0]);
            }

            playlistitems.Add
            (
                new PlaylistItem
                {
                    playlist = p,
                    PlaylistName = playlist.DisplayName,
                    NumOfVid = p.Files.Count.ToString(),
                    Thumbnail = image
                }
            );
        }
    }
}

现在下面是我在异步事件处理程序中使用它的代码,并且它导致我的问题。

    private async void ME_MediaOpened(object sender, RoutedEventArgs e)
    {

        //When used here, it just skippes everything in the method
        //as i described on the comments in code above.

        LoadPlaylists();

        //after skipping the compiler comes here and tries to execute
        //the lines below, which obviously causes exceptions because
        above method was never completed to begin with.

        var sd = playlistitems.Count;
        //some more code
    }

1 个答案:

答案 0 :(得分:3)

您应该返回Task而不是void,以便等待方法完成。