Threading Model IN WPF ------------ Consolidated from MSDN

原创 2007年09月30日 15:25:00

No matter how well architected, no UI framework will ever be able to provide a single threaded solution for every sort of problem. WPF comes close, but there are still situations where multiple threads improve user interface (UI) responsiveness or application performance. After discussing some background material, this paper will explore some of these situations. We’ll finish with a discussion of some lower level details for the particularly curious.

Overview and the Disptacher
Typical, WPF applications start their lives with two threads: one for handling rendering and another for managing the UI. The rendering thread effectively runs hidden in the background while the UI thread receives input, handles events, paints the screen, and runs application code. Most applications use a single UI thread, although in some situations it is best to use several. We’ll discuss this with an example later in the paper.

The UI thread queues work items inside an object called a Dispatcher. The Dispatcher selects work items on a priority basis and runs each one to completion. Every UI thread must have at least one Dispatcher and each Dispatcher can execute work items in exactly one thread.

The trick to building responsive, friendly applications is to maximize the Dispatcher throughput by keeping the work items small. This way items never get stale sitting in the Dispatcher queue waiting for processing. Any perceivable delay between input and response can frustrate a user.

How then are WPF applications supposed to handle big operations? What if my code involves a large calculation or needs to query a database on some remote server? Usually, the answer is to handle the big operation in a separate thread, leaving the UI thread free to tend to items in the Dispatcher queue. When the big operation completes, it can report its result back to the UI thread for display.

Historically, Windows only allows UI elements to be accessed by the thread that created them. This means that a background thread in charge of some long running task can’t update a textbox when it’s done. Windows does this to ensure the integrity of UI components. A listbox could turn out looking very strange if its contents were updated by a background thread during painting.

WPF has a built in mutual exclusion mechanism that enforces this. Just about every class in WPF descends from DisptcherObject. At construction, a DisptcherObject stores a reference to the Dispatcher linked to the currently running thread. In effect, the DisptcherObject associates with the thread that creates it. During program execution, a DisptcherObject can call its public VerifyAccess method. VerifyAccess examines the Dispatcher associated with the current thread and compares it to the Dispatcher reference stored during construction. If they don’t match, VerifyAccess throws an exception. VerifyAccess is intended to be called at the beginning of every method belonging to a DisptcherObject.

If only one thread can modify the UI how do background threads interact with the user? A background thread can ask the UI thread to perform an operation on its behalf. It does this by registering a work item with the Dispatcher of the UI thread. The Dispatcher class provides two methods for registering work items: Invoke and BeginInvoke. Both methods schedule a delegate for execution. Invoke is a synchronous call – that is, it doesn’t return until the UI thread actually finishes executing the delegate. BeginInvoke is asynchronous and returns immediately.

the Dispatcher orders the elements in its queue by priority. There are ten levels that may be specified when adding an element to the Dispatcher queue. These priorities are maintained in the DispatcherPriority enumeration. Detailed information about DispatcherPriority levels can be found in the Windows SDK documentation. 



<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<ProgressBar Margin="120,12,99,15" Name="progressBar1" Value="100" />
<Button Grid.Row="1" Margin="147,17,155,10" Name="button1" Click="button1_Click">Button</Button>


public partial class Window1 : System.Windows.Window
public delegate void AscCall();

public Window1()

void button1_Click(object sender, RoutedEventArgs e)
/**//* The first way of AscCall */
            AscCall acall 
= new AscCall(DoLargeBlock);

/**//* The second way of AscCall */
//ThreadStart ts = new ThreadStart(DoLargeBlock);
//Thread td = new Thread(ts);

private void DoLargeBlock()

private void UpdateUI()
new AscCall(UpdatePrograbass));
new AscCall(UpdateButton));

private void UpdatePrograbass()
= 50;

private void UpdateButton()
this.button1.Content = "Done";



技术总监问我,在注册表关于组件多线程设置,在服务器找到已注册dll发现ThreadingModel键值,真不知什么意思,以下文章我看了真不懂,反正发现值是free,是支持多线程,以下备忘留用。   ...
  • wonitazansa1
  • wonitazansa1
  • 2012年05月16日 16:07
  • 1916


  • elfc2000
  • elfc2000
  • 2011年02月11日 23:23
  • 3872

2.9. Threading Model

Java thread pool API The Apache Camel threading model is based on the powerful Java concurrency API...
  • cietli
  • cietli
  • 2017年03月12日 17:27
  • 73

COM线程模型详解         线程模型是一种数学模型,专门针对多线程编程而...
  • weiweixiaopasta
  • weiweixiaopasta
  • 2016年04月07日 10:44
  • 520

Threading in C#.pdf

  • 2007年11月11日 23:24
  • 431KB
  • 下载

python in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored错误

  • womengdoushizhongguo
  • womengdoushizhongguo
  • 2015年01月13日 12:32
  • 1863

WPF 性能分析工具【From MSDN】

WPF 提供了一套性能分析工具,来帮助您分析应用程序的运行时行为,并确定可以应用的性能优化的类型。下表列出了 Windows SDK 工具 WPFPerf 中包括的五个性能分析工具:工具说明Perfo...
  • Maths_bai
  • Maths_bai
  • 2010年03月26日 15:58
  • 2813


【权限维持】window服务端常见后门技术 0x00 前言   未知攻焉知防,攻击者在获取服务器权限后,通常会用一些后门技术来维持服务器权限,服务器一旦被植入后门,攻击者如入无...
  • m0_37438418
  • m0_37438418
  • 2018年01月24日 15:16
  • 83


一.创建一个接口 struct ISimpleMsgBox : public IUnknown { // IUnknown STDMETHOD_(ULONG, AddRef)() P...
  • zhoujiaxq
  • zhoujiaxq
  • 2014年06月19日 16:56
  • 932

Windows 窗体控件和等效的 WPF 控件(源MSDN)

Windows 窗体控件和等效的 WPF 控件(源MSDN)WPF学习 2008-11-13 14:20:26 阅读5 评论0 字号:大中小 下表显示哪些 Windows 窗体控件和组件具有等效的 W...
  • cs_victor
  • cs_victor
  • 2010年05月26日 14:08
  • 890
您举报文章:Threading Model IN WPF ------------ Consolidated from MSDN