WPF中的多进程(Threading)处理实例(一)
说明:希望通过揣摩这些案例,能进一步了解进程的工作原理。
1.方法一描述的是在同一窗口中,在计算素数的同时而不影响Canvas的工作。
- #region Long-Running Calculation in UI Thread
- public delegate void NextPrimeDelegate();
- private long num = ;
- private bool continueCalculating = false;
- private bool fNotAPrime = false;
- private void btnPrimeNumber_Click(object sender, RoutedEventArgs e)
- {
- if (continueCalculating)
- {
- continueCalculating = false;
- btnPrimeNumber.Content = "Resume";
- }
- else
- {
- continueCalculating = true;
- btnPrimeNumber.Content = "Stop";
- //获取与此 System.Windows.Threading.DispatcherObject 关联的 System.Windows.Threading.Dispatcher
- //public DispatcherOperation BeginInvoke(DispatcherPriority priority, Delegate method);
- //按指定的优先级在与 System.Windows.Threading.Dispatcher 关联的线程上异步执行指定的委托。
- btnPrimeNumber.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new NextPrimeDelegate(CheckNextNumber));
- }
- }
- public void CheckNextNumber()
- {
- // Reset flag.
- fNotAPrime = false;
- for (long i = ; i <= Math.Sqrt(num); i++)
- {
- if (num % i == )
- {
- // Set not-a-prime flag to true.
- fNotAPrime = true;
- break;
- }
- }
- // If a prime number.
- if (!fNotAPrime)
- {
- tbPrime.Text = num.ToString();
- }
- num += ;
- if (continueCalculating)
- {
- //3.In the CheckNextNumber function, because the first parameter
- //passed into BeginInvoke is DispatcherPriority.SystemIdle(在系统空闲时处理操作。),
- //all of the CheckNextNumber workitem will not break the UI operation.
- btnPrimeNumber.Dispatcher.BeginInvoke(
- System.Windows.Threading.DispatcherPriority.SystemIdle,
- new NextPrimeDelegate(this.CheckNextNumber));
- }
- }
- #endregion
- #region Blocking Operation in Worker Thread
- private delegate void NoArgDelegate();
- private delegate void OneArgDelegate(Int32[] arg);
- //1.When the Retrieve Data from Server button is clicked, the click handle retrieveData function is called.
- private void btnRetrieveData_Click(object sender, RoutedEventArgs e)
- {
- this.btnRetrieveData.IsEnabled = false;
- this.btnRetrieveData.Content = "Contacting Server";
- NoArgDelegate fetcher = new NoArgDelegate(this.RetrieveDataFromServer);
- //2.Then our codes use delegate.BeginInvoke to start a thread from the thread pool.
- //This thread is used to perform the long operation of retrieving data.
- fetcher.BeginInvoke(null, null);
- }
- /// <summary>
- /// Retrieve data in a worker thread(辅助线程).
- /// </summary>
- private void RetrieveDataFromServer()
- {
- //3.We use Thread.Sleep(5000) to simulate(模拟) a 5 seconds delay here.
- // Simulate the delay from network access.
- Thread.Sleep();
- //4.The codes generate 4 random numbers as data and update them to the UI by calling the Dispatcher.BeginInvoke().
- // Generate random data to be displayed.
- Random rand = new Random();
- Int32[] data = {
- rand.Next(), rand.Next(),
- rand.Next(), rand.Next()
- };
- // Schedule the update function in the UI thread.
- this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
- new OneArgDelegate(UpdateUserInterface), data);
- }
- /// <summary>
- /// Update the UI about the new data. The function runs in the UI thread.
- /// </summary>
- /// <param name="data"></param>
- private void UpdateUserInterface(Int32[] data)
- {
- this.btnRetrieveData.IsEnabled = true;
- this.btnRetrieveData.Content = "Retrieve Data from Server";
- this.tbData1.Text = data[].ToString();
- this.tbData2.Text = data[].ToString();
- this.tbData3.Text = data[].ToString();
- this.tbData4.Text = data[].ToString();
- }
- #endregion
WPF中的多进程(Threading)处理实例(一)的更多相关文章
- WPF中的多进程(Threading)处理实例(二)
原文:WPF中的多进程(Threading)处理实例(二) //错误的处理 private void cmdBreakRules_Click(object sender, RoutedEventArg ...
- 线程在WPF中的使用
项目中可能会有这样的需求,一直获取新的某个数据信息,但仍不影响其他的操作功能,这时就用到了线程,获取新数据放到线程中操作,对其他操作不产生影响,下面就以随机获取数组中项为例解说WPF中使用线程这一实例 ...
- WPF中未将对象引用设置到对象的实例
前几天,我开始了WPF的基础学习,一上来我就遇到了一个令我头痛的问题,按照书上的例子我写了一段属于自己的代码,一个简单的色调器.满心期待的编译运行,就出现了未将对象引用设置到对象的实例.我在网上查阅了 ...
- WPF中,如何将绑定源设置到单件实例
原文:WPF中,如何将绑定源设置到单件实例 WPF中,如何将绑定源设置到单件实例 周银辉 大概两个月前,曾有位朋友问我:如 ...
- WPF中元素拖拽的两个实例
今天结合之前做过的一些拖拽的例子来对这个方面进行一些总结,这里主要用两个例子来说明在WPF中如何使用拖拽进行操作,元素拖拽是一个常见的操作,第一个拖拽的例子是将ListBox中的子元素拖拽到ListV ...
- Python中的多进程与多线程(二)
在上一章中,学习了Python多进程编程的一些基本方法:使用跨平台多进程模块multiprocessing提供的Process.Pool.Queue.Lock.Pipe等类,实现子进程创建.进程池(批 ...
- Python中使用多进程来实现并行处理的方法小结
进程和线程是计算机软件领域里很重要的概念,进程和线程有区别,也有着密切的联系,先来辨析一下这两个概念: 1.定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和 ...
- 聊聊Python中的多进程和多线程
今天,想谈一下Python中的进程和线程. 最近在学习Django的时候,涉及到了多进程和多线程的知识点,所以想着一下把Python中的这块知识进行总结,所以系统地学习了一遍,将知识梳理如下. 1. ...
- Windows 消息循环(2) - WPF中的消息循环
接上文: Windows 消息循环(1) - 概览 win32/MFC/WinForm/WPF 都依靠消息循环驱动,让程序跑起来. 本文介绍 WPF 中是如何使用消息循环来驱动程序的. 4 消息循环在 ...
随机推荐
- Kinect 摄像头范围介绍和玩家舒适距离实测
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接: http://blog.csdn.net/cartzhang/article/details/44588097 作者:ca ...
- [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 ...
- [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 ...
- Java设计模式菜鸟系列(二十二)中介者模式建模与实现
转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/40027109 中介者模式(Mediator):主要用来减少类与类之间的耦合的,由于假设类与 ...
- PPT之SmartArt功能
在PPT中,我们经常看到这样的漂亮的组合图标: 他们是怎么做出来的呢?其实用ppt自带的SmartArt功能就能做出来了. Tips:SmartArt可以直接先选择组合图标再填文字,还可以写好了文字, ...
- java-线程-生产者-消费者
概述 在Java中有四种方法支持同步,其中前三个是同步方法,一个是管道方法. wait() / notify()方法 await() / signal()方法 BlockingQueue阻塞队列方法 ...
- solrj 7.x Expected mime type application/octet-stream but got text/html.
出现这种情况是因为baseurl填写错误,最开始的时候我写的是用tomcat启动后浏览器中访问solr的地址 结果就出现了如题的异常,当然提示的是404,还有可能提示405,Method not al ...
- 浏览器加载js文件顺序
在默认情况下,下载和执行js都会阻塞页面的渲染,当然现在浏览器支持并行下载,但仍然会阻塞图片等的下载和渲染,所以通常建议把js文件放body底.对于执行顺序,不管是外部js还是内部,只要 遇到< ...
- 【JDBC】java PreparedStatement操作oracle数据库
************************************************************************ ****原文:blog.csdn.net/clark_ ...
- JSON.parse(JSON.stringify()) 实现对对象的深度拷贝,从而互不影响
JSON.parse(JSON.stringify({"key": "value"})) 根据不包含引用对象的普通数组深拷贝得到启发,不拷贝引用对象,拷贝一个字 ...