TaskBarProgress (task bar progress bar)

Original: TaskBarProgress (task bar progress bar)

 1  <Window.TaskbarItemInfo>
2 <TaskbarItemInfo />
3 </Window.TaskbarItemInfo>
4
5 <Grid>
6 <ProgressBar x:Name="ProgressBar" Margin="10" 7 HorizontalAlignment="Stretch" 8 VerticalAlignment="Center" 9 Height="23"/>10</Grid>



 

 1 public partial class MainWindow : Window
2 {
3 public MainWindow()
4 {
5 InitializeComponent();
6
7 Loaded += new RoutedEventHandler(MainWindow_Loaded);
8 }
9
10 void MainWindow_Loaded(object sender, RoutedEventArgs e)
11 {
12 //BackgroundWorker,在单独的线程上执行操作13 BackgroundWorker worker = new BackgroundWorker();14


15 // BW.DoWork, occurs when BW.RunWorkerAsync() is called
16 17 // Initialize the named delegate; the left side of the Lambda operator is the input parameter (if any), the right side contains the expression or statement block; 18 worker.DoWork += (s, ev) => 19 { 20 for ( int i = 0 ; i < 100 ; i += 10 ) 21 { 22 Thread.Sleep( 1000 ); 23 24 // BW.ReportProgress, raise BW.ProgressChanged event 25 ((BackgroundWorker)s).ReportProgress(i); 26 }










27 };
28 // Occurs when BW.ReportProgress is called 29 worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); 30 31 // Occurs when the background operation is completed, cancelled or an exception is thrown 32 worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( worker_RunWorkerCompleted); 33 34 // BackgroundWorker.WorkerReportsProgress Gets or sets a value indicating whether the BackgroundWorker can report progress updates. 35 worker.WorkerReportsProgress = true ; 36 37 // TaskbarItemInfo Gets or sets the taskbar thumbnail of System.Windows.Window.









38 // TaskbarItemInfo.ProgressState Gets or sets a value indicating how to display progress indicators in taskbar buttons. 39 40 TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate; 41 42 // TaskbarItemProgressState enumeration value 43 // None=0, Indeterminate=1 (flashing green), Normal=2 (green), Error=3 (red), Paused=4 (yellow) 44 45 // Start performing background operations 46 worker.RunWorkerAsync(); 47 48 } 49 50 51 void worker_ProgressChanged( object sender, ProgressChangedEventArgs e) 52 { 53 //














ProgressChangedEventArgs.ProgressPercentage Gets the progress percentage of the asynchronous task 54 ProgressBar.Value = e.ProgressPercentage; 55 56 // double TaskbarItemInfo.ProgressValue Gets or sets a value that indicates the full state of the progress indicator in the taskbar button. 57 TaskbarItemInfo.ProgressValue = ( double )e.ProgressPercentage / 100 ; 58 } 59 60 void worker_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e) 61 { 62 ProgressBar.Value = 100 ; 63 TaskbarItemInfo.ProgressValue = 1.0









;
64 }
65 }




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325214366&siteId=291194637