C# WPF 不确定进度条

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39196709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:55:08  来源:igfitidea点击:

C# WPF Indeterminate progress bar

c#wpf

提问by edb500

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 = falseand 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;
    }));
}

回答by Massimiliano Kraus

Your code can't work because the "do some work" is happening on the same Threadon which the UI works. So, if that Threadis busy with the "work", how can it handle the UI animation for the ProgressBarat the same time? You have to put the "work" on another Thread, so the UI Threadis free and can do its job with the ProgressBar(or other UI controls).

您的代码无法工作,因为“做一些工作”发生在ThreadUI 工作的同一个地方。那么,如果那Thread是忙于“工作”,它如何同时处理 UI 动画ProgressBar呢?你必须把“工作”放在另一个上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) 创建一个完成工作并返回 a 的方法Task,因此可以“等待”完成:

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

2) Put the asyncmodifier 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 DoWorkAsyncthis way:

3)扔掉所有“调度程序”/“调用”等东西,并使用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_Clickencounters the first await... it begins to work automatically on another Thread, leaving the UI Threadfree to operate and animate the ProgressBar. When the work is finished, the control of the method returns to the UI Threadthat sets the IsIndeterminateto false.

那么,现在发生了什么?当GenerateCSV_Click遇到第一个await......它开始自动工作在另一个Thread,留下UI Thread自由操作和动画ProgressBar。工作完成后,该方法的控制权返回到将UI Thread设置IsIndeterminate为 false 的 。

Here you can find the MSDN tutorial on asyncprogramming: https://msdn.microsoft.com/en-us/library/mt674882.aspxIf 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如果您在 Google 上搜索“C# async await”,您会找到许多教程、示例和解释; )