How to Pause an Application without Freezing the U

2019-09-02 04:09发布

问题:

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.