锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / nVLC / WPF
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883

WPF ProgressBar


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 Valueproperty 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 ProgressBarand 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'sSetValue method, so it will have the exact same signature as well.

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

Visual Basic:

'Create a Delegate that matches  
'the Signature of the ProgressBar's SetValue method  
Private Delegate Sub UpdateProgressBarDelegate(ByVal dp As _
System.Windows.DependencyProperty, _
ByVal value As Object)    
Private Sub Process()        
'Configure the ProgressBar      
ProgressBar1.Minimum = 0      
ProgressBar1.Maximum = Short.MaxValue
ProgressBar1.Value = 0        
'Stores the value of the ProgressBar
Dim value As Double = 0        
'Create a new instance of our ProgressBar Delegate that points      
' to the ProgressBar's SetValue method.
Dim updatePbDelegate As New _          
UpdateProgressBarDelegate(AddressOf ProgressBar1.SetValue)        
'Tight Loop: Loop until the ProgressBar.Value reaches the max      
Do Until ProgressBar1.Value = ProgressBar1.Maximum             
value += 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(updatePbDelegate, _
System.Windows.Threading.DispatcherPriority.Background, _              
New Object() {ProgressBar.ValueProperty, value})
Loop    
End Sub

C#

//Create a Delegate that matches   
//the Signature of the ProgressBar's SetValue method  
private delegate void UpdateProgressBarDelegate(          
System.Windows.DependencyProperty dp, Object value);      
private void Process()  
{      
//Configure the ProgressBar      
ProgressBar1.Minimum = 0;      
ProgressBar1.Maximum = short.MaxValue;      
ProgressBar1.Value = 0;        
//Stores the value of the ProgressBar      
double value = 0;        
//Create a new instance of our ProgressBar Delegate that points      
// to the ProgressBar's SetValue method.      
UpdateProgressBarDelegate updatePbDelegate =           
new UpdateProgressBarDelegate(ProgressBar1.SetValue);        
//Tight Loop: Loop until the ProgressBar.Value reaches the max      
do      
{          
value += 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(updatePbDelegate,               
System.Windows.Threading.DispatcherPriority.Background,               
new object[] { ProgressBar.ValueProperty, value });      
}      
while (ProgressBar1.Value != ProgressBar1.Maximum);  
}

Conclusion

This is a very brief, and hopefully clear and concise article on using a WPF ProgressBar. The source code for both Visual Basic and C# is included for you as a reference.

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内