Cool .NET Tips and Tricks #9
By Dr. Dexter Dotnetsky
Printer - Friendly Version
Dr. Dotnetsky

Howdy it's Dr. Dotnetsky again. Didn't I say "I'll be back"? Actually, I have the solution to the California problem. (Of course, I've known it all along). Just let 'em secede from the Union and form their own country. Then they can issue Treasury securities just like Uncle Sam and do all the deficit spending they want, legally. End of problem! And Hollywood with all its tax-and-spend anti-war liberals will be happier than a buncha pigs in mud! (Of course, we could get lucky and there will be a huge earthquake, and the whole damn mess will fall into the Pacific Ocean - a lot cleaner solution, in Dr. Dotnetsky's opinion!)



Maximizing Performance of Web Controls

This week I have a couple of interesting snippets. One was sparked by an article in MSDN online by Scott Mitchell regarding ASP.NET WebControls performance. If you have a DataGrid or similar control that is used for display only, a good trick is to NOT place it inside a <FORM> tag set in the ASPX portion of your page. The control will work just fine. If you look at the difference in page size for a WebForm.aspx page that displays a subset of the employees table from the Northwind database in a page inside a Datagrid, the "non-form" method results in a page size of 10,359 bytes, while the page where the DataGrid tags are placed inside the default <FORM> tags generated by the ASPX page template is 35,823 bytes - more than three times the size! The vast majority of this difference is taken up by the _VIEWSTATE field and its contents. (You can also disable ViewState either for the Page or for the control, but I believe you'll still have a stub). This is true of almost any WebControl in the ASP.NET collection.

Working with WMI and Performance Counters

Putting Performance Counter data into your applications, whether Winforms or WebForms - based apps, is very easy with the .NET Framework. However, the Performance Monitoring Provider is not registered by default; you need to write some MOF code and execute it with the MOFCOMP.EXE compiler utility first. To register the Performance Monitoring provider as an instance provider:

Using mofcomp.exe, compile the following MOF code in a DOS box with:
mofcomp -N:root\default yourmof.mof


instance of __Win32Provider as $PMPInst
{
Name = "PerfProv";
ClsId = "{f00b4404-f8f1-11ce-a5b6-00aa00680c3f}";
HostingModel = "NetworkServiceHost";
};

instance of __InstanceProviderRegistration
{
Provider = $PMPInst;
SupportsPut = FALSE;
SupportsGet = TRUE;
SupportsDelete = FALSE;
SupportsEnumeration = TRUE;
};

To use the provider to show, for example, the Percent Processor Time on your WebServer in a web page, you would use code like the following (Be aware that the first call to NextValue() results in a zero, so you must call it at least twice):

System.Diagnostics.PerformanceCounter myCounter =
new System.Diagnostics.PerformanceCounter();
myCounter.CategoryName = "Processor";
myCounter.CounterName = "% Processor Time";
myCounter.InstanceName = "_Total";
float raw = myCounter.NextValue();
raw = myCounter.NextValue();
Label1.Text="Processor: " + raw.ToString();

 

If you are interested in exploring all the WMI counters and other items provided by these classes, just open up the Server Explorer and expand the "Performance Counters" Node. As can be seen in the image below, you can actually right click on a specific provider instance and add it directly to the Designer. This even works in an ASP.NET app.


You can also bring up a custom dialog with the Properties choice that gives you lots of interesting options.

Dr. Dexter Dotnetsky is the alter-ego of the Eggheadcafe.com forums, where he often pitches in to help answer particularly difficult questions and make snide comments. Dr. Dotnetsky holds no certifications, and does not have a resume. Always the consummate gentleman, Dr. Dotnetsky can be reached at youbetcha@mindless.com.  Dr. Dotnetsky's motto: "If we were all meant to get along, there would be no people who wait for all the groceries to be rung up before starting to look for their damn checkbook."