Partager via


Weather Service Simulation Via Dispatcher Sample

This sample demonstrates how to keep a blocking operation from making an application unresponsive.

This sample simulates a weather service application which queries a remote resource. While the application is querying the remote resource, the UI should not be unresponsive. To solve this, a background thread retrieves the information. When the thread has completed its task, it pushes a job onto dispatcher of the UI thread.

A background thread is created by calling BeginInvoke on the delegate object. This is an asynchronous call that uses a threadpool thread, so there is no other work we need to do with regards to thread creation.

The delay of connecting to the remote service is simulated by putting the worker thread to sleep. When the job is finished it pushes another job onto the Dispatcher of the UI thread to update the UI with the weather forecast information.

This sample also demonstrates animation and storyboards. Most of the animation is defined in the XAML file, while the starting and stopping of the animation is handled in the code behind.

See the Windows Presentation Foundation Threading Model topic for a discussion on this sample.

This sample demonstrates a specific feature of the Windows Presentation Foundation and, consequently, does not follow application development best practices. For comprehensive coverage of Windows Presentation Foundation (WPF) and Microsoft .NET Framework application development best practices, refer to the following as appropriate:

Accessibility - Accessibility Best Practices

Localization - WPF Globalization and Localization Overview

Performance - Optimizing WPF Application Performance

Security - WPF Security

Download sample

Building the Sample

  • Install the Windows Software Development Kit (SDK) and open its build environment command window. On the Start menu, point to All Programs, Microsoft Windows SDK, and then click CMD Shell.

  • Download the sample, usually from the software development kit (SDK) documentation, to your hard disk drive.

  • To build the sample from the build environment command window, go to the source directory of the sample. At the command prompt, type MSBUILD.

  • To build the sample in Microsoft Visual Studio 2005, load the sample solution or project file and then press CTRL+SHIFT+B.

Running the Sample

  • To run the compiled sample from the build environment command window, execute the .exe file in the Bin\Debug or Bin\Release folder contained under the sample source code folder.

  • To run the compiled sample with debugging in Visual Studio 2005, press F5.

See Also

Other Resources

Windows Presentation Foundation Threading Model
Single Threaded Application With Long Running Calculation Sample
Multithreading Web Browser Sample
Disable Command Source Via System Timer Sample