C#WPF不确定进度条 [英] C# WPF Indeterminate progress bar

查看:43
本文介绍了C#WPF不确定进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人建议为什么以下方法不起作用?我想要做的就是显示一个不确定的进度条,单击该按钮时它会启动,然后在完成一些工作之后,我将不确定的进度条设置为false来停止它.

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.

但是,当我在不确定的进度条下面运行代码时,甚至无法启动.我尝试注释掉 this.progressBar.IsIndeterminate = 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.

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

推荐答案

您的代码无法正常工作,因为执行某些工作"发生在与UI相同的 Thread 上.因此,如果 Thread 忙于工作",它如何同时处理 ProgressBar 的UI动画?您必须将工作"放在另一个 Thread 上,因此 UI Thread 是免费的,并且可以使用 ProgressBar (或其他)执行其工作.UI控件).

Your code can't work because the "do some work" is happening on the same Thread on which the UI works. So, if that Thread is busy with the "work", how can it handle the UI animation for the ProgressBar at the same time? 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).

1)创建一个可以完成工作并返回 Task 的方法,因此可以等待"完成:

1) create a method that does the work and returns a Task, so it can be "awaited" for completion:

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

2)将 async 修饰符放在 GenerateCSV_Click 上:

2) Put the async modifier on the GenerateCSV_Click:

private async void GenerateCSV_Click(object sender, RoutedEventArgs e)

3)丢弃所有调度程序"/调用"等内容,并以这种方式使用 DoWorkAsync :

3) throw away all that "Dispatcher" / "Invoke" etc stuffs, and use the DoWorkAsync this way:

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

    await DoWorkAsync();

    PB.IsIndeterminate = false;
}

那么,现在发生了什么?当 GenerateCSV_Click 遇到第一个 await ...时,它将开始自动在另一个 Thread 上工作,而保留 UI线程免费操作和动画 ProgressBar .工作完成后,该方法的控制权返回到 UI线程,该线程将 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. When the work is finished, the control of the method returns to the UI Thread that sets the IsIndeterminate to false.

在这里您可以找到有关 async 编程的MSDN教程: https://msdn.microsoft.com/en-us/library/mt674882.aspx如果您使用Google"C#async await",您会发现许多教程,示例和说明;)

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 ;)

这篇关于C#WPF不确定进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆