Wpf 異步方法更新進度條


前面用了Background方法來更新進度條,這次用更好用異步方法來更新進度條

先看效果

 

cs代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading;
 7 using System.Threading.Tasks;
 8 using System.Windows;
 9 using System.Windows.Controls;
10 using System.Windows.Data;
11 using System.Windows.Documents;
12 using System.Windows.Input;
13 using System.Windows.Media;
14 using System.Windows.Media.Imaging;
15 using System.Windows.Navigation;
16 using System.Windows.Shapes;
17 
18 namespace Wpf中的異步操作
19 {
20     /// <summary>
21     /// MainWindow.xaml 的交互邏輯
22     /// </summary>
23     public partial class MainWindow : Window
24     {
25         public MainWindow()
26         {
27             InitializeComponent();
28         }
29 
30         private void Button_Click(object sender, RoutedEventArgs e)
31         {
32             Update();
33         }
34 
35         private async void Button_Click1(object sender, RoutedEventArgs e)
36         {
37             text.Text = "0";
38             var result = await UpdateAsync();//等待方法完成
39             text.Text = result.ToString();
40             MessageBox.Show("異步方法完成后");//在方法完成后輸出
41         }
42 
43         private void Button_Click2(object sender, RoutedEventArgs e)
44         {
45             text.Text = "0";
46             UpdateAsync().ContinueWith(t =>
47             {
48                 //UI線程調用更新文字
49                 Dispatcher.Invoke(() =>
50                 {
51                     text.Text = t.Result.ToString();
52                 });
53                 MessageBox.Show("異步方法完成后");
54             });//沒有等待,繼續往下執行
55 
56             MessageBox.Show("異步方法完成前");//在方法完成前輸出
57         }
58 
59         /// <summary>
60         /// 同步方法更新進度條
61         /// </summary>
62         private void Update()
63         {
64             for (int i = 0; i < 100; i++)
65             {
66                 Thread.Sleep(50);
67                 bar.Value = i + 1;
68             }
69         }
70 
71         /// <summary>
72         /// 異步方法更新進度條
73         /// </summary>
74         /// <returns></returns>
75         private async Task<int> UpdateAsync()
76         {
77             var num = 0;
78             for (int i = 0; i < 100; i++)
79             {
80                 await Task.Delay(50);//等待
81                 bar.Value = i + 1;
82                 num += i;
83             }
84             return num;
85         }
86     }
87 }

xaml代碼

 1 <Window
 2     x:Class="Wpf中的異步操作.MainWindow"
 3     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 4     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 6     xmlns:local="clr-namespace:Wpf中的異步操作"
 7     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 8     Title="MainWindow"
 9     Width="800"
10     Height="450"
11     mc:Ignorable="d">
12     <Grid>
13         <Grid.ColumnDefinitions>
14             <ColumnDefinition />
15         </Grid.ColumnDefinitions>
16         <StackPanel Orientation="Vertical">
17             <Button
18                 Width="200"
19                 Height="50"
20                 Click="Button_Click"
21                 Content="同步方法" />
22             <Button
23                 Width="200"
24                 Height="50"
25                 Click="Button_Click1"
26                 Content="異步方法1" />
27             <Button
28                 Width="200"
29                 Height="50"
30                 Click="Button_Click2"
31                 Content="異步方法2" />
32             <TextBlock
33                 HorizontalAlignment="Center"
34                 FontSize="50"
35                 Text="{Binding Path=Value, ElementName=bar}" />
36             <ProgressBar x:Name="bar" Height="50" />
37             <TextBlock
38                 x:Name="text"
39                 HorizontalAlignment="Center"
40                 FontSize="50"
41                 Text="0" />
42         </StackPanel>
43     </Grid>
44 </Window>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM