Let’s Tech @ NTU 2011
WPF及其线程管理
by 赵楚昕
1
什么是WPF
Windows Presentation Foundation (or WPF) is a
computer-software graphical subsystem for
rendering user interfaces in Windows-based
applications (Wikipedia).
Windows Presentation Foundation (WPF) is a
next-generation presentation system for
building Windows client applications with
visually stunning user experiences. With WPF,
you can create a wide range of both standalone
and browser-hosted applications (Microsoft).
2
Silverlight Showcase
Official Site
http://www.silverlight.net/showcase/
btyazilim
http://www.btyazilim.com.tr/
Microsoft Academic Seaerch
http://academic.research.microsoft.com/VisualEx
plorer.aspx#63591
3
Winform vs. WPF
成熟度( 更
多资源)
Winform
4
Winform vs. WPF
相对分辨率
工具 向量图像
Silverlight Databinding
相对较新
(较少资源) WPF 硬件加速
5
Winform vs. WPF
简单的实例对比
*正版开发工具可以从以下网站下载
• DreamSpark (All NTU Students)
• MSDNAA (All SCE Students)
6
什么是线程
线程(Thread),是操作系统可以进行调
度的最小单位。
进程 1
线程 线程 线程 线程
…
1 2 3 4
7
为什么要线程管理
在学校里,
Web-based
One thread per request
Handled by sever-side software
Apache HTTP server
Internet Information Services (IIS)
Apache Tomcat
Console-based
多数情况为单线程
我们往往直接忽略线程的存在
8
为什么要线程管理
实际开发中,
GUI 开发
Web server 的底层优化
操作系统的开发
线程与其相关问题其实无处不在
9
.NET Framework 中如何线程
1. Thread t = new Thread(delegate() { foo(); });
2. ThreadPool.QueueUserWorkItem((WaitCall
back)delegate(object s) { foo(); });
3. Dispatcher.BeginInvoke((Action)delegate() {
foo(); });
4. Dispatcher.Invoke((Action)delegate() {
foo();});
5. …
* 注意delegation的使用(下划线部分)
10
Thread t = new …
Concurrent programming 基础
Synchronization
Deadlock
Debug
Support
11
.NET Framework 中的线程模式
ThreadPool
Dispatcher
BackgroundWorker
优势
绝大部线程开发中可能出现的问题都已经被 .NET
Framework 解决。
限制
后台线程(Background Thread)
前台线程的终止会造成运行中的后台线程直接终止
12
Let’s CODE!
Quick WPF Project Demo
新建项目
XMAL
Drag and drop
Writing markup
Events handling
Threading demo, the Bad
Threading demo, the Good
13
Q & A
14
Reference
Windows Presentation Foundation
http://msdn.microsoft.com/en-us/library/ms754130.aspx
WPF Architecture
http://msdn.microsoft.com/en-us/library/ms750441.aspx
WPF Threading Model
http://msdn.microsoft.com/en-us/library/ms741870.aspx
Build More Responsive Apps With The Dispatcher
http://msdn.microsoft.com/en-us/magazine/cc163328.aspx
Generic BackgroundWorker implementation in C#:
http://osherove.com/blog/2004/6/16/backgroundworker-implementation-in-c.html
Why Threads Are A Bad Idea
http://www.cs.sfu.ca/~vaughan/teaching/431/papers/ousterhout-threads-usenix96.pdf
Simple WPF Progress Window with Cancellation
http://blog.quantumbitdesigns.com/2008/07/22/simple-wpf-progress-window-with-
cancellation/
They are Anonymous Methods, not Anonymous Delegates
http://staceyw1.wordpress.com/2007/12/22/they-are-anonymous-methods-not-
anonymous-delegates/
15