原文:WPF中的多进程(Threading)处理实例(一)

说明:希望通过揣摩这些案例,能进一步了解进程的工作原理。

1.方法一描述的是在同一窗口中,在计算素数的同时而不影响Canvas的工作。

方法1

  1. #region Long-Running Calculation in UI Thread
  2.  
  3. public delegate void NextPrimeDelegate();
  4. private long num = ;
  5. private bool continueCalculating = false;
  6. private bool fNotAPrime = false;
  7.  
  8. private void btnPrimeNumber_Click(object sender, RoutedEventArgs e)
  9. {
  10. if (continueCalculating)
  11. {
  12. continueCalculating = false;
  13. btnPrimeNumber.Content = "Resume";
  14. }
  15. else
  16. {
  17. continueCalculating = true;
  18. btnPrimeNumber.Content = "Stop";
  19.  
  20. //获取与此 System.Windows.Threading.DispatcherObject 关联的 System.Windows.Threading.Dispatcher
  21.  
  22. //public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method);
  23. //按指定的优先级在与 System.Windows.Threading.Dispatcher 关联的线程上异步执行指定的委托。
  24. btnPrimeNumber.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(CheckNextNumber));
  25.  
  26. }
  27. }
  28.  
  29. public void CheckNextNumber()
  30. {
  31. // Reset flag.
  32. fNotAPrime = false;
  33.  
  34. for (long i = ; i <= Math.Sqrt(num); i++)
  35. {
  36. if (num % i == )
  37. {
  38. // Set not-a-prime flag to true.
  39. fNotAPrime = true;
  40. break;
  41. }
  42. }
  43.  
  44. // If a prime number.
  45. if (!fNotAPrime)
  46. {
  47. tbPrime.Text = num.ToString();
  48. }
  49.  
  50. num += ;
  51.  
  52. if (continueCalculating)
  53. {
  54. //3.In the CheckNextNumber function, because the first parameter
  55. //passed into BeginInvoke is DispatcherPriority.SystemIdle(在系统空闲时处理操作。),
  56. //all of the CheckNextNumber workitem will not break the UI operation.
  57.  
  58. btnPrimeNumber.Dispatcher.BeginInvoke(
  59. System.Windows.Threading.DispatcherPriority.SystemIdle,
  60. new NextPrimeDelegate(this.CheckNextNumber));
  61. }
  62. }
  63.  
  64. #endregion
方法2

  1. #region Blocking Operation in Worker Thread
  2.  
  3. private delegate void NoArgDelegate();
  4. private delegate void OneArgDelegate(Int32[] arg);
  5.  
  6. //1.When the Retrieve Data from Server button is clicked, the click handle retrieveData function is called.
  7. private void btnRetrieveData_Click(object sender, RoutedEventArgs e)
  8. {
  9. this.btnRetrieveData.IsEnabled = false;
  10. this.btnRetrieveData.Content = "Contacting Server";
  11.  
  12. NoArgDelegate fetcher = new NoArgDelegate(this.RetrieveDataFromServer);
  13.  
  14. //2.Then our codes use delegate.BeginInvoke to start a thread from the thread pool.
  15. //This thread is used to perform the long operation of retrieving data.
  16. fetcher.BeginInvoke(null, null);
  17. }
  18.  
  19. /// <summary>
  20. /// Retrieve data in a worker thread(辅助线程).
  21. /// </summary>
  22. private void RetrieveDataFromServer()
  23. {
  24. //3.We use Thread.Sleep(5000) to simulate(模拟) a 5 seconds delay here.
  25. // Simulate the delay from network access.
  26. Thread.Sleep();
  27.  
  28. //4.The codes generate 4 random numbers as data and update them to the UI by calling the Dispatcher.BeginInvoke().
  29.  
  30. // Generate random data to be displayed.
  31. Random rand = new Random();
  32. Int32[] data = {
  33. rand.Next(), rand.Next(),
  34. rand.Next(), rand.Next()
  35. };
  36.  
  37. // Schedule the update function in the UI thread.
  38. this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
  39. new OneArgDelegate(UpdateUserInterface), data);
  40.  
  41. }
  42.  
  43. /// <summary>
  44. /// Update the UI about the new data. The function runs in the UI thread.
  45. /// </summary>
  46. /// <param name="data"></param>
  47. private void UpdateUserInterface(Int32[] data)
  48. {
  49. this.btnRetrieveData.IsEnabled = true;
  50. this.btnRetrieveData.Content = "Retrieve Data from Server";
  51. this.tbData1.Text = data[].ToString();
  52. this.tbData2.Text = data[].ToString();
  53. this.tbData3.Text = data[].ToString();
  54. this.tbData4.Text = data[].ToString();
  55. }
  56.  
  57. #endregion

WPF中的多进程(Threading)处理实例(一)的更多相关文章

  1. WPF中的多进程(Threading)处理实例(二)

    原文:WPF中的多进程(Threading)处理实例(二) //错误的处理 private void cmdBreakRules_Click(object sender, RoutedEventArg ...

  2. 线程在WPF中的使用

    项目中可能会有这样的需求,一直获取新的某个数据信息,但仍不影响其他的操作功能,这时就用到了线程,获取新数据放到线程中操作,对其他操作不产生影响,下面就以随机获取数组中项为例解说WPF中使用线程这一实例 ...

  3. WPF中未将对象引用设置到对象的实例

    前几天,我开始了WPF的基础学习,一上来我就遇到了一个令我头痛的问题,按照书上的例子我写了一段属于自己的代码,一个简单的色调器.满心期待的编译运行,就出现了未将对象引用设置到对象的实例.我在网上查阅了 ...

  4. WPF中,如何将绑定源设置到单件实例

    原文:WPF中,如何将绑定源设置到单件实例  WPF中,如何将绑定源设置到单件实例                                       周银辉 大概两个月前,曾有位朋友问我:如 ...

  5. WPF中元素拖拽的两个实例

    今天结合之前做过的一些拖拽的例子来对这个方面进行一些总结,这里主要用两个例子来说明在WPF中如何使用拖拽进行操作,元素拖拽是一个常见的操作,第一个拖拽的例子是将ListBox中的子元素拖拽到ListV ...

  6. Python中的多进程与多线程(二)

    在上一章中,学习了Python多进程编程的一些基本方法:使用跨平台多进程模块multiprocessing提供的Process.Pool.Queue.Lock.Pipe等类,实现子进程创建.进程池(批 ...

  7. Python中使用多进程来实现并行处理的方法小结

    进程和线程是计算机软件领域里很重要的概念,进程和线程有区别,也有着密切的联系,先来辨析一下这两个概念: 1.定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和 ...

  8. 聊聊Python中的多进程和多线程

    今天,想谈一下Python中的进程和线程. 最近在学习Django的时候,涉及到了多进程和多线程的知识点,所以想着一下把Python中的这块知识进行总结,所以系统地学习了一遍,将知识梳理如下. 1. ...

  9. Windows 消息循环(2) - WPF中的消息循环

    接上文: Windows 消息循环(1) - 概览 win32/MFC/WinForm/WPF 都依靠消息循环驱动,让程序跑起来. 本文介绍 WPF 中是如何使用消息循环来驱动程序的. 4 消息循环在 ...

随机推荐

  1. Kinect 摄像头范围介绍和玩家舒适距离实测

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/44588097 作者:ca ...

  2. [React] Normalize Events with Reacts Synthetic Event System

    Event handlers are passed an instance of SyntheticEvent in React. In this video we'll take a look at ...

  3. [Ramda] Refactor to a Point Free Function with Ramda's useWith Function

    Naming things is hard and arguments in generic utility functions are no exception. Making functions ...

  4. Java设计模式菜鸟系列(二十二)中介者模式建模与实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/40027109 中介者模式(Mediator):主要用来减少类与类之间的耦合的,由于假设类与 ...

  5. PPT之SmartArt功能

    在PPT中,我们经常看到这样的漂亮的组合图标: 他们是怎么做出来的呢?其实用ppt自带的SmartArt功能就能做出来了. Tips:SmartArt可以直接先选择组合图标再填文字,还可以写好了文字, ...

  6. java-线程-生产者-消费者

    概述 在Java中有四种方法支持同步,其中前三个是同步方法,一个是管道方法. wait() / notify()方法 await() / signal()方法 BlockingQueue阻塞队列方法 ...

  7. solrj 7.x Expected mime type application/octet-stream but got text/html.

    出现这种情况是因为baseurl填写错误,最开始的时候我写的是用tomcat启动后浏览器中访问solr的地址 结果就出现了如题的异常,当然提示的是404,还有可能提示405,Method not al ...

  8. 浏览器加载js文件顺序

    在默认情况下,下载和执行js都会阻塞页面的渲染,当然现在浏览器支持并行下载,但仍然会阻塞图片等的下载和渲染,所以通常建议把js文件放body底.对于执行顺序,不管是外部js还是内部,只要 遇到< ...

  9. 【JDBC】java PreparedStatement操作oracle数据库

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  10. JSON.parse(JSON.stringify()) 实现对对象的深度拷贝,从而互不影响

    JSON.parse(JSON.stringify({"key": "value"})) 根据不包含引用对象的普通数组深拷贝得到启发,不拷贝引用对象,拷贝一个字 ...