How to Pause an Application without Freezing the UI

南笙酒味 提交于 2019-12-11 19:03:37

问题


On my click event I want to show an indeterminate progressbar for a certain amount of time, and then resume the application. The problem I am having is that I cannot get the progressbar to show. Could someone help with this?

private void RunTestButton_Click(object sender, RoutedEventArgs e)
    {
        if (testRunning == false)
            {
                testRunning = true;

                //Set progress bar visibility
                PerformanceProgressbar.Visibility = Visibility.Visible;
                PerformanceProgressbar.IsIndeterminate = true;

                //Tell the app to pause for 5 seconds so the user sees the progress bar


                //Set progress bar visibility
                PerformanceProgressbar.IsIndeterminate = false;
                PerformanceProgressbar.Visibility = Visibility.Collapsed;

                testRunning = false;
            }
    }

回答1:


You can call Task.Delay .. and await the result:

await Task.Delay(5000);

You will need to make sure you mark your event as async too:

private async void RunTestButton_Click(object sender, RoutedEventArgs e)



回答2:


PerformanceProgressbar is control, so you should put it on your page (use fade also).

But if you need to do some task on UI thread (do you really need it?) I recommend you to fade your page by showing semitransparent border over it, and show ProgressIndicator in statusbar. It's system object and will run even if app is frozen.



来源:https://stackoverflow.com/questions/22926978/how-to-pause-an-application-without-freezing-the-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!