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.

486 ❘ ChaPTer 19 instrumentAtiOn<br />

var counters =<br />

new CounterCreationDataCollection(counterCreationData);<br />

var category = PerformanceCounterCategory.Create(<br />

perfomanceCounterCategoryName,<br />

"Sample Counters for Professional <strong>C#</strong>",<br />

PerformanceCounterCategoryType.MultiInstance,<br />

counters);<br />

}<br />

MessageBox.Show(String.Format(<br />

"category {0} successfully created",<br />

category.CategoryName));<br />

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

adding PerformanceCounter Components<br />

With Windows Forms or Windows Service applications, you can add PerformanceCounter components<br />

from the toolbox or from the Server Explorer with drag <strong>and</strong> drop to the designer surface.<br />

With WPF applications that’s not possible. However, it’s not a lot of work to define the performance counters<br />

manually, as this is done with the method InitializePerformanceCounts(). Here, the CategoryName for<br />

all performance counts is set from the const string performanceCounterCategoryName; the CounterName<br />

is set from the sorted list. Because the application writes performance counts, the ReadOnly property<br />

must be set to false. When writing an application that just reads performance counts for display<br />

purposes, you can use the default value of the ReadOnly property, which is true. The InstanceName of<br />

the PerformanceCounter object is set to an application name. If the counters are configured to be global<br />

counts, the InstanceName may not be set.<br />

private PerformanceCounter performanceCounterButtonClicks;<br />

private PerformanceCounter performanceCounterButtonClicksPerSec;<br />

private PerformanceCounter performanceCounterMouseMoveEvents;<br />

private PerformanceCounter performanceCounterMouseMoveEventsPerSec;<br />

private void InitializePerformanceCounts()<br />

{<br />

performanceCounterButtonClicks = new PerformanceCounter<br />

{<br />

CategoryName = perfomanceCounterCategoryName,<br />

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

ReadOnly = false,<br />

MachineName = ".",<br />

InstanceLifetime = PerformanceCounterInstanceLifetime.Process,<br />

InstanceName = this.instanceName<br />

};<br />

performanceCounterButtonClicksPerSec = new PerformanceCounter<br />

{<br />

CategoryName = perfomanceCounterCategoryName,<br />

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

ReadOnly = false,<br />

MachineName = ".",<br />

InstanceLifetime = PerformanceCounterInstanceLifetime.Process,<br />

InstanceName = this.instanceName<br />

};<br />

performanceCounterMouseMoveEvents = new PerformanceCounter<br />

{<br />

CategoryName = perfomanceCounterCategoryName,<br />

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

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!