简体   繁体   English

C#WPF不确定进度条

[英]C# WPF Indeterminate progress bar

Please could someone suggest why the following doesn't work? 请有人建议为什么以下不起作用? All I want to do is display an indeterminate progress bar which starts when the button is clicked, then after I've done some work I set the indeterminate progress bar to false to stop it. 我想要做的就是显示一个不确定的进度条,它在单击按钮时启动,然后在我完成一些工作后,我将不确定的进度条设置为false以停止它。

However when I run the code below the indeterminate progress bar doesn't even start. 但是,当我运行下面的代码时,不确定的进度条甚至没有启动。 I tried commenting out the line this.progressBar.IsIndeterminate = false and when I do this the progress bar does start but then doesn't terminate. 我尝试将this.progressBar.IsIndeterminate = false注释掉,当我这样做时,进度条会启动,但不会终止。

private void GenerateCSV_Click(object sender, RoutedEventArgs e)
{
    this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate ()
    {
        this.progressBar.IsIndeterminate = true;

        // do some work
        Thread.Sleep(10 * 1000);

        this.progressBar.IsIndeterminate = false;
    }));
}

Your code can't work because the "do some work" is happening on the same Thread on which the UI works. 您的代码无法工作,因为“做一些工作”发生在UI工作的同一个Thread上。 So, if that Thread is busy with the "work", how can it handle the UI animation for the ProgressBar at the same time? 因此,如果该Thread忙于“工作”,它如何同时处理ProgressBar的UI动画? You have to put the "work" on another Thread , so the UI Thread is free and can do its job with the ProgressBar (or other UI controls). 您必须将“work”放在另一个Thread ,因此UI Thread是免费的,可以使用ProgressBar (或其他UI控件)完成其工作。

1) create a method that does the work and returns a Task , so it can be "awaited" for completion: 1)创建一个方法来完成工作并返回一个Task ,因此可以“等待”完成:

private async Task DoWorkAsync()
{
    await Task.Run(() =>
    {
        //do some work HERE
        Thread.Sleep(2000);
    });
}

2) Put the async modifier on the GenerateCSV_Click : 2)将async修饰符放在GenerateCSV_Click

private async void GenerateCSV_Click(object sender, RoutedEventArgs e)

3) throw away all that "Dispatcher" / "Invoke" etc stuffs, and use the DoWorkAsync this way: 3)抛弃所有“Dispatcher”/“Invoke”等东西,并以这种方式使用DoWorkAsync

private async void Button_Click(object sender, RoutedEventArgs e)
{
    PB.IsIndeterminate = true;

    await DoWorkAsync();

    PB.IsIndeterminate = false;
}

So, what's happening now? 那么,现在发生了什么? When GenerateCSV_Click encounters the first await ... it begins to work automatically on another Thread , leaving the UI Thread free to operate and animate the ProgressBar . GenerateCSV_Click遇到第一个await ......它开始在另一个Thread上自动工作,让UI Thread自由运行并为ProgressBar设置动画。 When the work is finished, the control of the method returns to the UI Thread that sets the IsIndeterminate to false. 工作完成后,方法的控制权返回到UI Thread ,该UI ThreadIsIndeterminate设置为false。

Here you can find the MSDN tutorial on async programming: https://msdn.microsoft.com/en-us/library/mt674882.aspx If you Google "C# async await", you'll find dozens of tutorials, examples and explanations ;) 在这里你可以找到关于async编程的MSDN教程: https//msdn.microsoft.com/en-us/library/mt674882.aspx如果你谷歌“C#async await”,你会发现很多教程,例子和解释;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM