150 likes | 255 Views
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
E N D
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 • 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
Understanding Threading and Asynchronous Processing Threading: Thread affinity: You can only use a WPF object on the thread on which it was created • All WPF applications use two threads: • Managing the user interface • Rendering visual elements • The Dispatcher class handles thread affinity Implement asynchronous processing by using: • The Dispatcher class • The ThreadPool class • The BackgroundWorker class • The Task Parallel Library
Implementing Asynchronous Processing by Using the Dispatcher Class Two approaches: Schedule prioritized work items by using the Dispatcher's message loop 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();
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. }
Implementing Asynchronous Processing by Using the BackgroundWorker Class The RunWorkerCompleted and ProgressChanged event handlers are executed on the creation thread The DoWork delegate is executed on a background thread You must not try to manipulate UI objects in your DoWork delegate
Implementing Asynchronous Processing by Using the TPL Sequential foreach loop Sequential tasks Sequential LINQ Parallel foreach loop Parallel tasks Parallel LINQ foreach (var item in source) { DoSomething(item); } Parallel.ForEach( source, item => DoSomething(item)); Parallel.Invoke( () => DoSomething(), () => DoSomethingElse()); DoSomething(); DoSomethingElse(); varevenNums = from num in source where Compute(num) > 0 select num; varevenNums = from num in source.AsParallel() where Compute(num) > 0 select num;
Lesson 2: Implementing Responsive User Interfaces • Understanding Responsive User Interfaces • Selecting an Asynchronous Processing Approach
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
Selecting an Asynchronous Processing Approach 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 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 Waiting for a process to return: • Use the ThreadPool class • Use the Task Parallel Library • Use the BackgroundWorker class
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
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.
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?
Module Review and Takeaways • Review Questions • Common Issues and Troubleshooting Tips