Compartir a través de


Disable Command Source Via System Timer Sample

This sample shows how to enable and disable a command source via a Windows.System.Timers.Timer.

A Windows.System.Timers.Timer runs on a separate thread than the UI thread, so some extra work is needed to update the UI. Another way to solve this problem, and in most cases a better and more elegant way, is a use the DispatcherTimer. This is easier because the DispatcherTimer runs on the same thread as the UI Thread. For information, see the Disable Command Source Via Dispatcher Timer Sample sample. There are times though when you may need to use a class that doesn't run on the UI thread, so this sample shows how to accomplish this.

Command sources, such as the MenuItem class and the Button class, listen to the CanExecuteChanged event on the RoutedCommand they are attached to in order to determine when they need to query the command to see if the command can execute on the current command target. Command sources will typically disable themselves if the command cannot execute and enable themselves if the command can execute, such as when a MenuItem gray's itself out when the command cannot execute.

The CommandManager notifies the RoutedCommand via the RequerySuggested event that conditions have changed with the command target. The RoutedCommand raises the CanExecuteChanged event which the command source listens to. Normally, this notification mechanism is adequate, but there are some situations where the CommandManager is unaware that the conditions have changed on the command target and thus the RequerySuggested event is never raised and the command source never queries the RoutedCommand. In these situations the CommandManager can be forced to raise the RequerySuggested event by calling InvalidateRequerySuggested.

This sample creates a RoutedCommand that can be executed only when the seconds in the current time are greater than a target value. A a System.Windows.Timers.Timer is created that pushes a job on the Dispatcher of the UI thread every second. Since this job is scheduled on the Dispatcher of the UI thread, the UI thread will update the UI for us.

The method that is pushed onto the Dispatcher calls InvalidateRequerySuggested. This insures that the command source will receive the CanExecuteChanged event so that it can call the CanExecute method on the command.

For more information on commanding see the Commanding Overview.

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

Security - WPF Security

Localization - WPF Globalization and Localization Overview

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

Commanding Overview
Disable Command Source Via Dispatcher Timer Sample