马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

Wpf Backgroundworker

[复制链接]
查看1758 | 回复0 | 2019-9-17 11:30:38 | 显示全部楼层 |阅读模式
  1. <Window x:Class="WpfApp53.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:local="clr-namespace:WpfApp53"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Grid>
  10. <Grid.RowDefinitions>
  11. <RowDefinition Height="*"/>
  12. <RowDefinition Height="Auto"/>
  13. <RowDefinition Height="Auto"/>
  14. <RowDefinition Height="Auto"/>
  15. </Grid.RowDefinitions>
  16. <ListBox Grid.Row="0" x:Name="listBox1"/>
  17. <ProgressBar Grid.Row="1" x:Name="progressBar1" Height="20" Margin="5"/>
  18. <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="5">
  19. <TextBlock Text="Status: "/>
  20. <TextBlock x:Name="statusTextBox"/>
  21. </StackPanel>
  22. <Grid Grid.Row="3" Margin="10">
  23. <Grid.ColumnDefinitions>
  24. <ColumnDefinition Width="*"/>
  25. <ColumnDefinition Width="*"/>
  26. </Grid.ColumnDefinitions>
  27. <Button Grid.Column="0" x:Name="startBtn" Content="Start" Click="StartBtn_Click"/>
  28. <Button Grid.Column="1" x:Name="cancelBtn" Content="Cancel" Click="CancelBtn_Click"/>
  29. </Grid>
  30. </Grid>
  31. </Window>
复制代码
  1. 1 using System;
  2. 2 using System.Collections.Generic;
  3. 3 using System.ComponentModel;
  4. 4 using System.Linq;
  5. 5 using System.Text;
  6. 6 using System.Threading;
  7. 7 using System.Threading.Tasks;
  8. 8 using System.Windows;
  9. 9 using System.Windows.Controls;
  10. 10 using System.Windows.Data;
  11. 11 using System.Windows.Documents;
  12. 12 using System.Windows.Input;
  13. 13 using System.Windows.Media;
  14. 14 using System.Windows.Media.Imaging;
  15. 15 using System.Windows.Navigation;
  16. 16 using System.Windows.Shapes;
  17. 17
  18. 18 namespace WpfApp53
  19. 19 {
  20. 20 /// <summary>
  21. 21 /// Interaction logic for MainWindow.xaml
  22. 22 /// </summary>
  23. 23 public partial class MainWindow : Window
  24. 24 {
  25. 25 BackgroundWorker bgWorker;
  26. 26 public MainWindow()
  27. 27 {
  28. 28 InitializeComponent();
  29. 29 cancelBtn.IsEnabled = false;
  30. 30 }
  31. 31
  32. 32 private void StartBtn_Click(object sender, RoutedEventArgs e)
  33. 33 {
  34. 34 listBox1.Items.Clear();
  35. 35 startBtn.IsEnabled = false;
  36. 36 cancelBtn.IsEnabled = true;
  37. 37 bgWorker = new BackgroundWorker();
  38. 38 bgWorker.DoWork += BgWorker_DoWork;
  39. 39 bgWorker.ProgressChanged += BgWorker_ProgressChanged;
  40. 40 bgWorker.RunWorkerCompleted += BgWorker_RunWorkerCompleted;
  41. 41 bgWorker.WorkerReportsProgress = true;
  42. 42 bgWorker.WorkerSupportsCancellation = true;
  43. 43
  44. 44 int maxItems = 50;
  45. 45 progressBar1.Minimum = 1;
  46. 46 progressBar1.Maximum = 100;
  47. 47
  48. 48 statusTextBox.Text = "Starting...";
  49. 49 bgWorker.RunWorkerAsync(maxItems);
  50. 50 }
  51. 51
  52. 52 private void BgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  53. 53 {
  54. 54 if(e.Cancelled)
  55. 55 {
  56. 56 statusTextBox.Text = "Cancelled";
  57. 57 }
  58. 58 else
  59. 59 {
  60. 60 statusTextBox.Text = "Completed";
  61. 61 }
  62. 62 statusTextBox.IsEnabled = true;
  63. 63 cancelBtn.IsEnabled = false;
  64. 64 }
  65. 65
  66. 66 private void BgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
  67. 67 {
  68. 68 double percent = (e.ProgressPercentage * 100) / 50;
  69. 69 progressBar1.Value = Math.Round(percent, 0);
  70. 70 listBox1.Items.Add(new ListBoxItem { Content = e.ProgressPercentage + " item added" });
  71. 71 statusTextBox.Text = Math.Round(percent, 0) + "% percent completed";
  72. 72 }
  73. 73
  74. 74 private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
  75. 75 {
  76. 76 int? maxItems = e.Argument as int?;
  77. 77 for(int i=1;i<=maxItems.GetValueOrDefault();i++)
  78. 78 {
  79. 79 if(bgWorker.CancellationPending)
  80. 80 {
  81. 81 e.Cancel = true;
  82. 82 break;
  83. 83 }
  84. 84 Thread.Sleep(200);
  85. 85 bgWorker.ReportProgress(i);
  86. 86 }
  87. 87 }
  88. 88
  89. 89 private void CancelBtn_Click(object sender, RoutedEventArgs e)
  90. 90 {
  91. 91 bgWorker.CancelAsync();
  92. 92 startBtn.IsEnabled = true;
  93. 93 }
  94. 94 }
  95. 95 }
复制代码







来源:https://www.cnblogs.com/Fred1987/archive/2019/09/15/11523444.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则