博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF中的多进程(Threading)处理实例(二)
阅读量:6174 次
发布时间:2019-06-21

本文共 9498 字,大约阅读时间需要 31 分钟。

 

 

 

 

 

 

 

 

 

 

1     //错误的处理 2         private void cmdBreakRules_Click(object sender, RoutedEventArgs e) 3         { 4             Thread thread = new Thread(UpdateTextWrong); 5             thread.Start(); 6         } 7  8         private void UpdateTextWrong() 9         {10             txt.Text = "Here is some new text.";11         }12 13         //正确的处理14         private void cmdFollowRules_Click(object sender, RoutedEventArgs e)15         {16             Thread thread = new Thread(UpdateTextRight);17             thread.Start();18         }               19 20         private void UpdateTextRight()21         {22             this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,23                 (ThreadStart) delegate()24                 {25                     //txt.Text = "Here is some new text.";26                     UpdateText();27                 }28                 );29         }30 31         private void UpdateText()32         {33 34             txt.Text = "这是更新后的文本。";35         }

 

说明:public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method);其中DispatcherPriority是从-1到10 的枚举值,10优先级最高。在UpdateTextRight方法中的一下代码段

this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,                (ThreadStart)delegate()                {                    //txt.Text = "Here is some new text.";                    UpdateText();                }                );
可以用一下代码来替换,以增加代码的可读性
private delegate void UpdateTextDelegate();this.Dispatcher.BeginInvoke(DispatcherPriority.Normal,new UpdateTextDelegate(UpdateText));
其实,这与上篇实例中应用的方法是相同的。 2.BackgroundWorker 2.1FindPrimesInput
FindPrimesInput
1  public class FindPrimesInput 2     {         3         public int To 4         { get; set; } 5  6         public int From 7         { get; set; } 8  9         public FindPrimesInput(int from, int to)10         {11             To = to;12             From = from;13         }14 15     }
2.2 Worker
Worker
1  public class Worker 2     { 3         public static int[] FindPrimes(int fromNumber, int toNumber) 4         { 5             return FindPrimes(fromNumber, toNumber, null); 6         } 7  8         public static int[] FindPrimes(int fromNumber, int toNumber, System.ComponentModel.BackgroundWorker backgroundWorker) 9         {10             int[] list = new int[toNumber - fromNumber];11 12             // Create an array containing all integers between the two specified numbers.13             for (int i = 0; i < list.Length; i++)14             {15                 list[i] = fromNumber;16                 fromNumber += 1;17             }18 19 20             //find out the module for each item in list, divided by each d, where21             //d is < or == to sqrt(to)22             //if the remainder is 0, the nubmer is a composite, and thus23             //we mark its position with 0 in the marks array,24             //otherwise the number is a prime, and thus mark it with 125 26             //Math.Floor 返回小于或等于指定双精度浮点数的最大整数。27             int maxDiv = (int)Math.Floor(Math.Sqrt(toNumber));28 29             int[] mark = new int[list.Length];30 31             for (int i = 0; i < list.Length; i++)32             {33                 for (int j = 2; j <= maxDiv; j++)34                 {35 36                     if ((list[i] != j) && (list[i] % j == 0))37                     {38                         mark[i] = 1;39                     }40 41                 }42 43                 int iteration = list.Length / 100;44                 if ((i % iteration == 0) && (backgroundWorker != null))45                 {46                     //BackgroundWorker.CancellationPending获取一个值,指示应用程序是否已请求取消后台操作。47                     if (backgroundWorker.CancellationPending)48                     {49                         // Return without doing any more work.50                         return null;                      51                     }52 53                     //BackgroundWorker.WorkerReportsProgress 获取或设置一个值,该值指示 System.ComponentModel.BackgroundWorker 能否报告进度更新。54                     if (backgroundWorker.WorkerReportsProgress)55                     {56                         //float progress = ((float)(i + 1)) / list.Length * 100;57                         //BackgroundWorker.ReportProgress 引发 System.ComponentModel.BackgroundWorker.ProgressChanged 事件。58                         backgroundWorker.ReportProgress(i / iteration);59                         //(int)Math.Round(progress));60                     }61                 }62 63             }64 65             //create new array that contains only the primes, and return that array66             int primes = 0;67             for (int i = 0; i < mark.Length; i++)68             {69                 if (mark[i] == 0)70                     primes += 1;71 72             }73 74             int[] ret = new int[primes];75             int curs = 0;76             for (int i = 0; i < mark.Length; i++)77             {78                 if (mark[i] == 0)79                 {80                     ret[curs] = list[i];81                     curs += 1;82                 }83             }84 85             if (backgroundWorker != null && backgroundWorker.WorkerReportsProgress)86             {87                 backgroundWorker.ReportProgress(100);88             }89 90             return ret;91 92         }93 94         95     }
2.3BackgroundWorker XAML
XAML
1 
7
8
12
13 14
15
16
17
18
19
20
21
22
23
24
25
26 27 28
From:
29
1
30
To:
31
500000
32 33
35
38
41
42 43
Results:
44
46 47 48
52
53
2.4 CS code
1  public partial class BackgroundWorkerTest : System.Windows.Window 2     { 3         public BackgroundWorkerTest() 4         { 5             InitializeComponent(); 6             //BackgroundWorker 在单独的线程上执行操作 7             //Window.FindResource(object resourceKey)搜索具有指定密钥的资源,找不到则会引发异常 8             backgroundWorker = ((BackgroundWorker)this.FindResource("backgroundWorker")); 9         }10 11         private BackgroundWorker backgroundWorker;12 13         private void cmdFind_Click(object sender, RoutedEventArgs e)14         {15             // Disable the button and clear previous results.16             cmdFind.IsEnabled = false;17             cmdCancel.IsEnabled = true;18             lstPrimes.Items.Clear();            19 20             // Get the search range.21             int from, to;22             if (!Int32.TryParse(txtFrom.Text, out from))23             {24                 MessageBox.Show("Invalid From value.");25                 return;26             }27             if (!Int32.TryParse(txtTo.Text, out to))28             {29                 MessageBox.Show("Invalid To value.");30                 return;31             }32 33             // Start the search for primes on another thread.34             FindPrimesInput input = new FindPrimesInput(from, to);35             backgroundWorker.RunWorkerAsync(input);36         }37 38         private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)39         {40             // Get the input values.41             FindPrimesInput input = (FindPrimesInput)e.Argument;42 43             // Start the search for primes and wait.44             int[] primes = Worker.FindPrimes(input.From, input.To, backgroundWorker);45 46             //BackgroundWorker.CancellationPending 获取一个值,指示应用程序是否已请求取消后台操作。47             if (backgroundWorker.CancellationPending)48             {49                 e.Cancel = true;50                 return;51             }52 53             // Return the result.54             e.Result = primes;55         }56 57         private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)58         {59             if (e.Cancelled)60             {61                 MessageBox.Show("Search cancelled.");62             }63             else if (e.Error != null)64             {65                 // An error was thrown by the DoWork event handler.66                 MessageBox.Show(e.Error.Message, "An Error Occurred");67             }68             else69             {70                 int[] primes = (int[])e.Result;71                 foreach (int prime in primes)72                 {73                     lstPrimes.Items.Add(prime);74                 }                75             }76             cmdFind.IsEnabled = true;77             cmdCancel.IsEnabled = false;78             progressBar.Value = 0;79         }80 81         private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)82         {83             //ProgressChangedEventArgs.ProgressPercentage 获取异步任务的进度百分比84             progressBar.Value = e.ProgressPercentage;85         }86 87         private void cmdCancel_Click(object sender, RoutedEventArgs e)88         {89             //BackgroundWorker.CancelAsync() 请求取消挂起后的操作90             backgroundWorker.CancelAsync();91         }92     }
 

 

 

转载于:https://www.cnblogs.com/January/archive/2012/04/09/2438668.html

你可能感兴趣的文章
初识CSS选择器版本4
查看>>
[Hadoop in China 2011] 朱会灿:探析腾讯Typhoon云计算平台
查看>>
JavaScript之数组学习
查看>>
PHP 设置响应头来解决跨域问题
查看>>
CAS实现SSO单点登录原理
查看>>
博客园美化专用图片链接
查看>>
HDU_1969_二分
查看>>
高等代数葵花宝典—白皮书
查看>>
一种简单的图像修复方法
查看>>
基于DobboX的SOA服务集群搭建
查看>>
C#设计模式之装饰者
查看>>
[noip模拟20170921]模版题
查看>>
获取ip
查看>>
Spring Shell简单应用
查看>>
移动app可开发的意见于分析
查看>>
周总结7
查看>>
类似OutLook布局的开源控件XPanderControls
查看>>
Web前端工程师成长之路——知识汇总
查看>>
[2018-9-4T2]探索黑暗dark
查看>>
【学术信息】中科院2019年学术期刊分区-综合性期刊
查看>>