module 8 enhancing user interface responsiveness

15
Module 8 Enhancing User Interface Responsiveness

Upload: tasha-slater

Post on 30-Dec-2015

25 views

Category:

Documents


0 download

DESCRIPTION

Module 8 Enhancing User Interface Responsiveness. Module Overview. Implementing Asynchronous Processes Implementing Responsive User Interfaces. Lesson 1: Implementing Asynchronous Processes. Understanding Threading and Asynchronous Processing - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Module 8 Enhancing User Interface Responsiveness

Module 8

Enhancing User Interface

Responsiveness

Page 2: Module 8 Enhancing User Interface Responsiveness

Module Overview

• Implementing Asynchronous Processes

• Implementing Responsive User Interfaces

Page 3: Module 8 Enhancing User Interface Responsiveness

Lesson 1: Implementing Asynchronous Processes

• Understanding Threading and Asynchronous Processing

• Implementing Asynchronous Processing by Using the Dispatcher Class

• Implementing Asynchronous Processing by Using the ThreadPool Class

• Implementing Asynchronous Processing by Using the BackgroundWorker Class

• Implementing Asynchronous Processing by Using the TPL

Page 4: Module 8 Enhancing User Interface Responsiveness

Threading:Threading:

Understanding Threading and Asynchronous Processing

• All WPF applications use two threads:

• Managing the user interface

• Rendering visual elements

• The Dispatcher class handles thread affinity

Thread affinity: You can only use a WPF object on the thread on which it was created

Thread affinity: You can only use a WPF object on the thread on which it was created

Implement asynchronous processing by using:Implement asynchronous processing by using:

• The Dispatcher class

• The ThreadPool class

• The BackgroundWorker class

• The Task Parallel Library

• The Dispatcher class

• The ThreadPool class

• The BackgroundWorker class

• The Task Parallel Library

Page 5: Module 8 Enhancing User Interface Responsiveness

Two approaches:Two approaches:

Implementing Asynchronous Processing by Using the Dispatcher Class

Schedule prioritized work items by using the Dispatcher's message loop

myControl.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(UserInterfaceUpdate));

myControl.Dispatcher.BeginInvoke( DispatcherPriority.Background, new Action(UserInterfaceUpdate));

Schedule periodic execution of work items by using a DispatcherTimer

myTimer = new DispatcherTimer( DispatcherPriority.Background, myControl.Dispatcher);myTimer.Interval = TimeSpan.FromSeconds(2);myTimer.Tick += new EventHandler( delegate(object s, EventArgs a) { // Update the user interface. });myTimer.Start();

myTimer = new DispatcherTimer( DispatcherPriority.Background, myControl.Dispatcher);myTimer.Interval = TimeSpan.FromSeconds(2);myTimer.Tick += new EventHandler( delegate(object s, EventArgs a) { // Update the user interface. });myTimer.Start();

Page 6: Module 8 Enhancing User Interface Responsiveness

Implementing Asynchronous Processing by Using the ThreadPool Class

The ThreadPool class:The ThreadPool class:

• Provides a pool of worker threads

• Manages thread creation and reuse

• Has 250 worker threads per processor by default

• Queues work items until a thread becomes available

• Cannot access the UI thread

public static void Main(){ ThreadPool.QueueUserWorkItem( new WaitCallback(DoWork));} private static void DoWork( object parameter){ // Perform background task.}

public static void Main(){ ThreadPool.QueueUserWorkItem( new WaitCallback(DoWork));} private static void DoWork( object parameter){ // Perform background task.}

Page 7: Module 8 Enhancing User Interface Responsiveness

Implementing Asynchronous Processing by Using the BackgroundWorker Class

You must not try to manipulate UI objects in your DoWork delegateYou must not try to manipulate UI objects in your DoWork delegate

The RunWorkerCompleted and ProgressChanged event handlers are executed on the creation thread

The DoWork delegate is executed on a background thread

Page 8: Module 8 Enhancing User Interface Responsiveness

Parallel foreach loop

Parallel tasks

Parallel LINQ

Parallel foreach loop

Parallel tasks

Parallel LINQ

Sequential foreach loop

Sequential tasks

Sequential LINQ

Sequential foreach loop

Sequential tasks

Sequential LINQ

Implementing Asynchronous Processing by Using the TPL

foreach (var item in source){ DoSomething(item);}

foreach (var item in source){ DoSomething(item);}

Parallel.ForEach( source, item => DoSomething(item));

Parallel.ForEach( source, item => DoSomething(item));

DoSomething();DoSomethingElse();DoSomething();DoSomethingElse();

Parallel.Invoke( () => DoSomething(), () => DoSomethingElse());

Parallel.Invoke( () => DoSomething(), () => DoSomethingElse());

var evenNums = from num in source where Compute(num) > 0 select num;

var evenNums = from num in source where Compute(num) > 0 select num;

var evenNums = from num in source.AsParallel() where Compute(num) > 0 select num;

var evenNums = from num in source.AsParallel() where Compute(num) > 0 select num;

Page 9: Module 8 Enhancing User Interface Responsiveness

Lesson 2: Implementing Responsive User Interfaces

• Understanding Responsive User Interfaces

• Selecting an Asynchronous Processing Approach

Page 10: Module 8 Enhancing User Interface Responsiveness

Understanding Responsive User Interfaces

An unresponsive application:An unresponsive application:

• Does not respond to user input

• Will not process UI updates

• Will present the busy cursor

• May appear to have crashed

• May display (Not Responding) in title bar

• May present entirely black or white window contents

Page 11: Module 8 Enhancing User Interface Responsiveness

Selecting an Asynchronous Processing Approach

Performing too much work on the

UI thread:

Performing too much work on the

UI thread:

• Use the ThreadPool class

• Use the Task Parallel Library

• Use the BackgroundWorker class

Thread starvation:Thread starvation:

• Use the ThreadPool class

Waiting for a process to return:Waiting for a process to return:

• Use the ThreadPool class

• Use the Task Parallel Library

• Use the BackgroundWorker class

The BackgroundWorker class is the only approach that directly enables you to report progress and marshals onto the UI thread for progress and completion events

The BackgroundWorker class is the only approach that directly enables you to report progress and marshals onto the UI thread for progress and completion events

Page 12: Module 8 Enhancing User Interface Responsiveness

Lab: Enhancing Application Performance

• Exercise 1: Choosing an Asynchronous Programming Strategy

• Exercise 2: Implementing Asynchronous Operations

• Exercise 3: Parallelizing Tasks

Logon information

Estimated time: 60 minutes

Page 13: Module 8 Enhancing User Interface Responsiveness

Lab Scenario

You have been asked to create a master view for all work orders in the system. This large data set must be responsive in the UI at all times and must also provide filtering and sorting support.

You will evaluate different asynchronous programming techniques to determine the optimal approach. In addition, you must calculate some statistics for each data operation, which will be calculated in parallel.

Page 14: Module 8 Enhancing User Interface Responsiveness

Lab Review

Review Questions

• How do you update the UI when you implement a long-running task as an asynchronous operation?

• How do you perform multiple tasks concurrently?

Page 15: Module 8 Enhancing User Interface Responsiveness

Module Review and Takeaways

• Review Questions

• Common Issues and Troubleshooting Tips