Covers topics on the Microsoft Certification Exam for the .NET Framework (Exam 70-536, Microsoft .NET Framework - Application Development Foundation)

Tuesday, October 21, 2008

How do I retrieve information about the current process using C#?

I can think of many occasions when I have been diagnosing an issue on a customer machine and I find myself opening the task manager to gather valuable information about the application under question. If the customer will allow it, I will usually take the task manager one step further and use the Process Explorer from SysInternals.

But there has to be a better way, your application should be able to grab that information without using another application. And in fact it can by using the System.Diagnostics namespace. Conveniently this is also a requirement on the 70-536 exam in the "Manage system processes and monitor the performance of a .NET application by using the diagnostics functionality of the .NET Framework." section. Specifically the objective is to:

"Retrieve information about the current process"

This is made possible by the Process class within the System.Diagnostics class. Before we get into the code sample, it should be noted that a Process has a number of attributes. Many of these attributes are omitted to keep it short and sweet. Refer to the docs for the full list of Process attributes.

Current Process Info Example

using System;
using System.Diagnostics;

namespace CurrentProcessInfo
{
class Program
{
static void Main(string[] args)
{
//First lets get our parent process
Process my_process = Process.GetCurrentProcess();

// Now we'll spit out some pertinent info on our process
// Only a handful of the properties are referred to here,
// see the docs for the full list
Console.WriteLine("\nThe current process has the following attributes:");
Console.WriteLine("------------------------------------");
Console.WriteLine("Process Name: " + my_process.ProcessName);
Console.WriteLine("Window Title: " + my_process.MainWindowTitle);
Console.WriteLine("Running on: " + my_process.MachineName);
Console.WriteLine("Priority: " + my_process.PriorityClass.ToString());
Console.WriteLine("Responding: " + my_process.Responding);
Console.WriteLine("Virtual Memory: " + my_process.VirtualMemorySize64);
Console.WriteLine("------------------------------------");
}
}
}


Additional Resources
Process Class (Microsoft)
System.Diagnostics Namespace (Microsoft)

Monday, October 20, 2008

Set a break point through code with C#

If you have spent time debugging, you probably know you can click any code line to add a break point. If you have spent some more time you have probably even figured out how to set conditional breakpoints, as Sara Ford explains in this "tip of the day".

But did you know you can also cause a break point to trigger from within your code? It is actually pretty easy using the Debugger class within the System.Diagnostics name space. I learned about the Debugger class while looking over the specifics for the "Debug and trace a .NET Framework application by using the System.Diagnostics namespace." section of the 70-536 exam.

If this code is run outside of the debugger, the OS will ask you if you would like to debug the process.

Debugger Break form Code Example

using System;

namespace BreakFromCodeExample
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Thing are going fine...");
Console.WriteLine("then something goes wrong and we want to break");
Console.WriteLine("so we can investigate further...");

//This is the call that fires the break point event
System.Diagnostics.Debugger.Break();

Console.WriteLine("You are now free to place break points with or without a GUI.");
}
}
}

Additional Resources
Debugger Class (Microsoft)
System.Diagnostics Namespace (Microsoft)

Implemeting the IConfigurationSystem Interface

After a long hiatus... getting back in the writing zone....

Within the "Embedding configuration, diagnostic, management, and installation features into a .NET Framework application" section of Exam 70-536 we are instructed to "Implement IConfigurationSystem Interface".

This seemed like a fairly straight forward task so I immediately looked up the interface in the MSDN docs. However, upon reading about the interface, I think someone goofed. The documentation specifically says that the IConfigurationSystem Interface "
supports the .NET Framework infrastructure and is not intended to be used directly from your code."

So maybe we don't need to know too much about this interface after all. Just in case the error is on the side of the documentation, you need to implement two methods to support the IConfigurationSystem Interface
  • GetConfig
  • Init

GetConfig returns an object and takes configKey (String) as a parameter.
Init returns void and takes no parameters.

Additional Resources
IConfigurationSystem Interface (Microsoft)

Support This Site

LinkShare  Referral  Prg