• WPF Progressbar


    参考:http://www.codeproject.com/KB/WPF/WpfProgressBar.aspx

    Introduction

    This article demonstrates how to use a WPF ProgressBar in a "tight code loop".

    Background

    For years, progress bars have been very useful and very easy to use in regular Windows Forms applications. All that was necessary was to set the Minimum and Maximum properties, then incrementally modify the Value property to report the progress. When necessary, DoEvents() was inserted to allow the form to refresh and update the progress bar.

    WPF progress bars are conceptually the same as Windows progress bars; however, a very noticeable difference is that using standard coding techniques, the WPF Progress Bars do not update correctly while the application is processing. This creates a very undesirable effect, especially since the whole purpose of the progress bar is to... report the progress.

    There are many situations where "tight loops" are required in code, such as when data is being read from a file. A standard coding approach is to open the file, then loop through the entire file, reading either lines, characters, or bytes in each pass. A progress bar may be used to report the progress of this operation.

    Tight loops present a bit of a challenge for WPF progress bars, and the standard coding techniques do not produce the same results as they do in Windows Forms. Therefore, this article specifically addresses this issue, and demonstrates how to use a WPF ProgressBar in a "tight code loop".

    Using the code

    The WPF ProgressBar has a "SetValue" method that can be used to update the progress of the ProgressBar. However, this method will not cause the ProgresssBar to refresh when there is a tight coding loop. Therefore, it is necessary to use the Invoke method of the Dispatcher class to update the progress of the ProgressBar and cause it to refresh correctly on the form.

    The Dispatcher class provides services for managing the queue of work items for a thread.

    One of the arguments of the Dispatcher.Invoke method is a delegate. Since a delegate can be created and used to point to a specific method to invoke, we will create and use one that will point to the ProgressBar's SetValue method, so it will have the exact same signature as well.

    Here's the complete example in Visual Basic and C#:

    下面是测试代码:

    private void button1_Click(object sender, RoutedEventArgs e)
            {
                Action<DependencyProperty, double> UpdateProgressbarValue = (dp, value) => progressBar1.SetValue(dp, value);

                //Configure the ProgressBar
                progressBar1.Minimum = 0;
                progressBar1.Maximum = short.MaxValue;
                progressBar1.Value = 0;

                //Stores the value of the ProgressBar
                double myvalue = 0;

                //Tight Loop: Loop until the ProgressBar.Value reaches the max
                do
                {
                    myvalue += 1;

                    /*Update the Value of the ProgressBar:
                        1) Pass the "updatePbDelegate" delegate
                           that points to the ProgressBar1.SetValue method
                        2) Set the DispatcherPriority to "Background"
                        3) Pass an Object() Array containing the property
                           to update (ProgressBar.ValueProperty) and the new value */
                    Dispatcher.Invoke(UpdateProgressbarValue,
                        System.Windows.Threading.DispatcherPriority.Background,
                        ProgressBar.ValueProperty, myvalue);
                }
                while (progressBar1.Value != progressBar1.Maximum);
            }

  • 相关阅读:
    jQuery选择器
    CSS选择器性能分析
    JavaScript 之垃圾回收和内存管理
    六个字符,带你领略JavaScript (js的艺术编写)
    Redis(1) 简介以及linux环境下的安装
    Teamviewer被商业检测处理办法
    Linux 分配/home的磁盘空间给根目录
    vmware虚拟机安装Oracle Linux 出现Unable to open the image
    Vim编辑器常用命令
    Xshell连接虚拟机中的Ubuntu
  • 原文地址:https://www.cnblogs.com/bear831204/p/1874793.html
Copyright © 2020-2023  润新知