I've updated my PerformanceHelper class that I'll throw around certain code blocks to help trend or monitor performance. Before it was merely a PerformanceCounter wrapper with a few metrics, but now you can specify Console, Debug and/or Perfmon. You do this by using an XOR ( '|' ) in a new Constructor. Here's an example which outputs to Debug.WriteLine and PerfMon:
for (int index = 0; index < 10; index++)
{
using (new PerformanceHelper("ConsoleSample",
"Perfmon Sample", OutputOptions.Debug | OutputOptions.Perfmon))
{
Thread.Sleep(500);
}
}
The method that does the Output to Console and Debug is marked as [Conditional("DEBUG")], because you really should have a better strategy for performance monitoring in production and you don't want that hit for each block that you wrapped.
Note to Vista Developers: You will have problems creating custom performance counters at runtime based on registry access. To support this in a client based app, you will need to specify an application manifest and mark it with the following:
<requestedExecutionLevel level="requireAdministrator" />
For more information on application manifests,
read this.
Here's one of those real-world situations that no one bothers to demo. If you are building a form or have some sort of input your gathering from the user, you'll likely want the first input control Focused initially. You have a few ways to do this (in ascending order of recommendation):
- Call Focus() on the first control explicitly (meh).
- Iterate the controls collection and find the lowest TabIndex (terrible code smell).
- Do the following in the Load event:
this.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));