15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Performance Monitoring ❘ 487<br />

}<br />

ReadOnly = false,<br />

MachineName = ".",<br />

InstanceLifetime = PerformanceCounterInstanceLifetime.Process,<br />

InstanceName = this.instanceName<br />

};<br />

performanceCounterMouseMoveEventsPerSec = new PerformanceCounter<br />

{<br />

CategoryName = perfomanceCounterCategoryName,<br />

CounterName = perfCountNames["mouseSec"].Item1,<br />

ReadOnly = false,<br />

MachineName = ".",<br />

InstanceLifetime = PerformanceCounterInstanceLifetime.Process,<br />

InstanceName = this.instanceName<br />

};<br />

code snippet PerformanceCounterDemo/MainWindow.xaml.cs<br />

For the calculation of the performance values, you need to add the fields clickCountPerSec <strong>and</strong><br />

mouseMoveCountPerSec:<br />

public partial class MainWindow : Window<br />

{<br />

// Performance monitoring counter values<br />

private int clickCountPerSec = 0;<br />

private int mouseMoveCountPerSec = 0;<br />

Add an event h<strong>and</strong>ler to the Click event of the button <strong>and</strong> an event h<strong>and</strong>ler to the MouseMove event to the<br />

button, <strong>and</strong> add the following code to the h<strong>and</strong>lers:<br />

private void OnButtonClick(object sender, RoutedEventArgs e)<br />

{<br />

this.performanceCounterButtonClicks.Increment();<br />

this.clickCountPerSec++;<br />

}<br />

private void OnMouseMove(object sender, MouseEventArgs e)<br />

{<br />

}<br />

this.performanceCounterMouseMoveEvents.Increment();<br />

this.mouseMoveCountPerSec++;<br />

The Increment() method of the PerformanceCounter object increments the counter by one. If you need<br />

to increment the counter by more than one, for example, to add information about a byte count sent or<br />

received, you can use the IncrementBy() method. For the performance counts that show the value in<br />

seconds, just the two variables, clickCountPerSec <strong>and</strong> mouseMovePerSec, are incremented.<br />

To show updated values every second, add a DispatcherTimer to the members of the MainWindow:<br />

private DispatcherTimer timer;<br />

This timer is configured <strong>and</strong> started in the constructor. The DispatcherTimer class is a timer from the<br />

namespace System.Windows.Threading. For other than WPF applications, you can use other timers that<br />

are discussed in Chapter 20, “Threads, Tasks, <strong>and</strong> Synchronization.” The code that is invoked by the timer<br />

is defined with an anonymous method.<br />

public MainWindow()<br />

{<br />

InitializeComponent();<br />

InitializePerfomanceCountNames();<br />

InitializePerformanceCounts();<br />

if (PerformanceCounterCategory.Exists(perfomanceCounterCategoryName))<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!