如何在Windows应用商店应用中实现暂停?

时间:2014-12-22 04:31:49

标签: multithreading windows-store-apps async-await thread-sleep

我正在为Windows应用商店创建幻灯片应用。我希望在照片之间暂停3秒。

await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(3));

上面的代码对我来说很好,但是线程在大约30秒后用0代码完成。 我知道0意味着成功,但上面的代码是在一个永远的循环内(如果最终达到pic索引然后重置索引从开始,就像Windows Photoviewer)

如何实现所需的功能?

更新:这是代码

private async void slideshow()
    {
        while (current_pic_index < fileList.Count)
        {

            if (current_pic_index < 0)
            {   
                current_pic_index = fileList.Count - 1;
            }

            IRandomAccessStream fileStream = await fileList[current_pic_index].OpenAsync(Windows.Storage.FileAccessMode.Read);
            BitmapImage bi3 = new BitmapImage();
            bi3.SetSource(fileStream);
            ImageBrush myBrush = new ImageBrush();

            Grid DynamicGrid = new Grid();
            ContentRoot.Children.Add(DynamicGrid);

            DynamicGrid.Width = Window.Current.Bounds.Width;
            DynamicGrid.Height = Window.Current.Bounds.Height;
            DynamicGrid.HorizontalAlignment = HorizontalAlignment.Right;
            DynamicGrid.VerticalAlignment = VerticalAlignment.Bottom;
            DynamicGrid.Background = myBrush;

            DynamicGrid.ManipulationMode = ManipulationModes.TranslateX;
            DynamicGrid.ManipulationStarted += test_grid_ManipulationStarted;
            DynamicGrid.ManipulationCompleted += test_grid_ManipulationCompleted;

            await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(3));
            current_pic_index++;

            if(current_pic_index == fileList.Count)
            {
                current_pic_index = 0;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

你正在延迟UI线程,这绝不是一个好主意。您应该在新线程中运行此函数,并在需要时调用UI线程:

    private void slideshow()
    {
        Task.Run(async () =>
        {
            while (current_pic_index < fileList.Count)
            {

                if (current_pic_index < 0)
                {
                    current_pic_index = fileList.Count - 1;
                }

                IRandomAccessStream fileStream = await fileList[current_pic_index].OpenAsync(Windows.Storage.FileAccessMode.Read);

                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                {
                    BitmapImage bi3 = new BitmapImage();
                    bi3.SetSource(fileStream);
                    ImageBrush myBrush = new ImageBrush();

                    Grid DynamicGrid = new Grid();
                    ContentRoot.Children.Add(DynamicGrid);

                    DynamicGrid.Width = Window.Current.Bounds.Width;
                    DynamicGrid.Height = Window.Current.Bounds.Height;
                    DynamicGrid.HorizontalAlignment = HorizontalAlignment.Right;
                    DynamicGrid.VerticalAlignment = VerticalAlignment.Bottom;
                    DynamicGrid.Background = myBrush;

                    DynamicGrid.ManipulationMode = ManipulationModes.TranslateX;
                    DynamicGrid.ManipulationStarted += test_grid_ManipulationStarted;
                    DynamicGrid.ManipulationCompleted += test_grid_ManipulationCompleted;
                });

                await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(3));
                current_pic_index++;

                if (current_pic_index == fileList.Count)
                {
                    current_pic_index = 0;
                }
            }
        });
    }