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 消息循环在 ...
随机推荐
- 【U218】A-B
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给出一个A/B-C/D表示的分数减法算式,A,B,C,D均为不超过32767的正整数,求A/B-C/D ...
- 【codeforces 758B】Blown Garland
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 2015年工作中遇到的问题:81-90,标题党-Nginx与真实IP-Mybatis等
81."标题党"与"百度收录"问题. 很久以来,就发现那些"标题党"的收录和排名情况非常好,比如CSDN某篇文章,就随便一写,就排在了第一 ...
- plist文件无法打包进.a静态库中
问题: 之前一直在做静态库的编写与维护,也一直知道静态库的图片资源是没办法打进.a中的.可是突然有个想法.由于有非常多參数的配置是在一个plist文件里的.尽管也知道这是一个plist文件,可是想想和 ...
- js进阶 9-15 多选框如何限制选中数目
js进阶 9-15 多选框如何限制选中数目 一.总结 一句话总结: 1.多选框如何限制选中数目? 没点击选择一次,来统计现在总共选了多少个,如果超出,就给onclick事件返回false,取消oncl ...
- java使用mongoDB和freemarker注意事项
mongoDB和关系数据库的差异最大的优势就是表结构不正确,说:.弱事务.mongoDB以牺牲换来的交易进行更快的访问速度,有人说mongoDB对于内存太贪婪,事实上,它不会.随着windows版本, ...
- 学习OpenCV研究报告指出系列(二)源代码被编译并配有实例project
下载并安装CMake3.0.1 要自己编译OpenCV2.4.9的源代码.首先.必须下载编译工具,使用的比較多的编译工具是CMake. 以下摘录一段关于CMake的介绍: CMake是一个 ...
- 让Duilib多线程编程更easy
一.Duilib不能开发多线程程序? 记得非常久曾经就听有人说过Duilib的多线程支持性不好,原因是Duilib里面的控件是用数组管理的全局变量,不能进行多线程訪问,加锁非常麻烦.事实上这个说法是非 ...
- 给定正整数n,计算出n个元素的集合{1,2,....,n}能够划分为多少个不同的非空集合
给定正整数n,计算出n个元素的集合{1,2,....,n}能够划分为多少个不同的非空集合 附源码: #include<iostream> using namespace std; int ...
- vista/win7系统 红警/CS/星际争霸 局域网连接方法
昨晚,闲来无事,忽然想起打红警来,于是和宿舍舍友商量一起联机打红警, 可是在win7下不能联机红警,网上很多人都这么说,昨晚我折腾了2小时,终于解决了这个问题. win7系统是可以联机打红警的!!!! ...