Presentation is loading. Please wait.

Presentation is loading. Please wait.

Module 8 Enhancing User Interface Responsiveness.

Similar presentations


Presentation on theme: "Module 8 Enhancing User Interface Responsiveness."— Presentation transcript:

1 Module 8 Enhancing User Interface Responsiveness

2 Module Overview Implementing Asynchronous Processes Implementing Responsive User Interfaces

3 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

4 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 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

5 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();

6 Implementing Asynchronous Processing by Using 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. }

7 Implementing Asynchronous Processing by Using the BackgroundWorker Class You 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

8 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;

9 Lesson 2: Implementing Responsive User Interfaces Understanding Responsive User Interfaces Selecting an Asynchronous Processing Approach

10 Understanding Responsive User Interfaces 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

11 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: Use the ThreadPool class 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

12 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

13 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.

14 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?

15 Module Review and Takeaways Review Questions Common Issues and Troubleshooting Tips


Download ppt "Module 8 Enhancing User Interface Responsiveness."

Similar presentations


Ads by Google