Wpf 异步方法更新进度条

前面用了Background方法来更新进度条,这次用更好用异步方法来更新进度条

先看效果

Wpf 异步方法更新进度条

 

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>

 

上一篇:WPF 写一个提醒工具软件(完整项目)


下一篇:WPF 入门教程DockPanel介绍