A Place for C Sharpers/.Netters

I Will do coding till last moment of life-Kiran Patil

Archive for March, 2010

How to get CPU Usage/Memory Usage/Physical Memory

Posted by kiranpatils on March 6, 2010

Here are the few functions which will help you out in getting CPU Usage, Memory Usage and Physical Memory Using C#.NET[Proprietor: Yogesh Patel]:


/// <summary>
 /// Gets CPU Usage in %
 /// </summary>
 /// <returns></returns>
 private double getCPUUsage()
 {
 ManagementObject processor = new ManagementObject("Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
 processor.Get();

 return double.Parse(processor.Properties["PercentProcessorTime"].Value.ToString());
 }

 /// <summary>
 /// Gets memory usage in %
 /// </summary>
 /// <returns></returns>
 private double getMemoryUsage()
 {
 double memAvailable, memPhysical;

 PerformanceCounter pCntr = new PerformanceCounter("Memory", "Available KBytes");
 memAvailable = (double) pCntr.NextValue();

 memPhysical = getPhysicalMemory();

 return (memPhysical - memAvailable) * 100 / memPhysical;
 }

 /// <summary>
 /// Gets total physical memory
 /// </summary>
 /// <returns></returns>
 private double getPhysicalMemory()
 {
 ObjectQuery winQuery = new ObjectQuery("SELECT * FROM Win32_LogicalMemoryConfiguration");
 ManagementObjectSearcher searcher = new ManagementObjectSearcher(winQuery);

 double memory = 0;
 foreach (ManagementObject item in searcher.Get())
 {
 memory = double.Parse(item["TotalPhysicalMemory"].ToString());
 }
 return memory;
 }

Please note that on INTERNET there are so many functions available but this is most accurate and perfect. We are using it on our production servers.

Posted in .NET | Leave a Comment »

How to Debug XSLT on the fly

Posted by kiranpatils on March 6, 2010

Challenge:

one of my friend asked me how to Debug XSLT at runtime. Means if i have one XML and XSLT and my code does parsing of  XML at run time using XSLT using XSLCompiledTransform Class with some parameters which are coming dynamic. Here’s the way to do it:

Solution:

Basic requirement is that you MUST be using XSLCompiledTransform for Transformation. If you are not using it then use it. It’s really great class.

1. Locate your Transformation code.

2. locate your XslCompiledTransform class’s instantiation code e.g.

// Enable XSLT debugging. true is the guy who does the magic
XslCompiledTransform xslt = new XslCompiledTransform(true);

3. Now go to  your Transform method and put breakpoint.

xslt.Transform(sourceFile, null, outputStream); //PUT Breakpoint on me!

4. Run your code in DEBUG mode and when BreakPoint comes at Transform method press F11 for stepping in to the code.

Happy Debugging!

NOTE : Please disable debugging (XslCompiledTransform xslt = new XslCompiledTransform();) once your code is ready for deployment

Reference :

http://msdn.microsoft.com/en-us/library/ms255603(VS.80).aspx

Posted in XSLT | Tagged: | Leave a Comment »

TreeView.FindNode with PopulateOnDemand feature

Posted by kiranpatils on March 2, 2010

Challenge:

If you are using Tree View with PopulateOnDemand feature – for those who are new to PopulateOnDemand feature – It Indicates whether the node is populated dynamically for example if you have tree structure as below:

imageNow, Normally you can bind above tree view on page load directly with all child nodes. But if you have huge data then it will affect performance. So, to avoid performance issue we use PopulateOnDemand feature as name suggests Nodes will be populated on demand only[on click of + sign]. Now our new data binding code will load all root nodes only and set it’s PopulateOnDemand property to true as shown here:

Markup code

<asp:TreeView ID="LinksTreeView" Font-Names="Arial" ForeColor="Blue" OnTreeNodePopulate="PopulateNode"
           SelectedNodeStyle-Font-Bold="true" SelectedNodeStyle-ForeColor="Chocolate" runat="server">
       </asp:TreeView>

Code-Behind

protected void Page_Load(object sender, EventArgs e)
    {  
        if (!IsPostBack)
        {
            // bind first level tree
            TreeNode treeNode1 = GetTreeNode("1");
            treeNode1.Expanded = false;
            LinksTreeView.Nodes.Add(treeNode1);

            TreeNode treeNode2 = GetTreeNode("2");
            treeNode2.Expanded = false;
            LinksTreeView.Nodes.Add(treeNode2);

            TreeNode treeNode3 = GetTreeNode("3");
            treeNode2.Expanded = false;
            LinksTreeView.Nodes.Add(treeNode3);

            // collapse all nodes and show root nodes only
            LinksTreeView.CollapseAll();
        }
    }

private static TreeNode GetTreeNode(string nodeTextValue)
    {
        TreeNode treeNode = new TreeNode(nodeTextValue, nodeTextValue);
        treeNode.SelectAction = TreeNodeSelectAction.SelectExpand;
        treeNode.PopulateOnDemand = true;
        return treeNode;

    }

protected void PopulateNode(Object sender, TreeNodeEventArgs e)
    {

        // Call the appropriate method to populate a node at a particular level.
        switch (e.Node.Text)
        {
            case "1":
                // Populate the first-level nodes.
                e.Node.ChildNodes.Add(GetTreeNode("1.1"));
                e.Node.ChildNodes.Add(GetTreeNode("1.2"));
                e.Node.ChildNodes.Add(GetTreeNode("1.3"));
                break;
            case "2":
                // Populate the second-level nodes.               
                e.Node.ChildNodes.Add(GetTreeNode("2.1"));
                e.Node.ChildNodes.Add(GetTreeNode("2.2"));
                e.Node.ChildNodes.Add(GetTreeNode("2.3"));
                break;
            case "3":
                // Populate the third-level nodes.               
                e.Node.ChildNodes.Add(GetTreeNode("3.1"));
                e.Node.ChildNodes.Add(GetTreeNode("3.2"));
                e.Node.ChildNodes.Add(GetTreeNode("3.3"));
                break;
            case "1.1":
                // Populate the first-level nodes.
                e.Node.ChildNodes.Add(GetTreeNode("1.1.1"));
                e.Node.ChildNodes.Add(GetTreeNode("1.1.2"));
                e.Node.ChildNodes.Add(GetTreeNode("1.1.3"));
                break;
            case "1.1.1":
                // Populate the first-level nodes.
                e.Node.ChildNodes.Add(GetTreeNode("1.1.1.1"));
                e.Node.ChildNodes.Add(GetTreeNode("1.1.1.2"));
                e.Node.ChildNodes.Add(GetTreeNode("1.1.1.3"));
                break;
            default:
                // Do nothing.
                break;
        }
    }

Great! Now we are ready just run your application and see the power of PopulateOnDemand feature. Whenever you expand any node[by clicking on plus sign] it will call PopulateNode Method because in tree view’s markup we have binded it.

Now, the main problem is here. Suppose you have a requirement in which you have to find 1.1.1.1 Node –which is at path 1/1.1/1.1.1/1.1.1.1. You will shout and say use TreeView’s FindNode method and provide path like this :

LinksTreeView.FindNode(“1/1.1/1.1.1/1.1.1.1”);

can you pls give a try and check does it works? it won’t because I’ve already tried :) let me tell you why – Because 1.1.1.1 node is not loaded yet and it’s parent node 1.1.1 is also not loaded yet and so on still 1.1. Just 1 has been loaded. So, let’s see how we can solve this challenge:

Solution:

After goggling a bit and braining a lot i found the following way:

Main aim is to load 1.1.1.1 but it’s the last node and to load it all it’s parent’s should be first loaded. Here’s the simple way of doing it:

LinksTreeView.FindNode(“1”).Expand(); // it will find node and call expand method so it will load 1.1 which is first child of 1

LinksTreeView.FindNode(“1/1.1”).Expand();  //loads 1.1.1

LinksTreeView.FindNode(“1/1.1/1.1.1”).Expand(); //loads 1.1.1.1

LinksTreeView.FindNode(“1/1.1/1.1.1/1.1.1.1”).Expand(); //Cheers we reached there!

Now, above code is bit hard-coded and i am against of hard-coding so wrote one generic method which works for any level of node finding[txtPath is one textbox which provides path to find]

protected void btnSelect_Click(object sender, EventArgs e)
   {  
       //It will not work because as it is populateondemand
       //this call will never find node because of populateondemand
       TreeNode foundNode = LinksTreeView.FindNode(txtPath.Text);
       if (foundNode == null)
       {
           // Now i am doing different way
           string selecteValuePath = txtPath.Text;
           string[] selectedValues = selecteValuePath.Split(LinksTreeView.PathSeparator);

           string findValueQuey = string.Empty;
           for (int counter = 0; counter < selectedValues.Length; counter++)
           {
               string fValuePath = string.Empty; ;
               if (counter == 0)
               {
                   // store 1
                   fValuePath = selectedValues[counter];
               }
               else if (counter < selectedValues.Length)
               {
                   // now path is 1/1.1
                   fValuePath = findValueQuey.ToString()
                       + LinksTreeView.PathSeparator
                       + selectedValues[counter];
               }

               //1/1.1/1.1.1/1.1.1.1
               foundNode = LinksTreeView.FindNode(fValuePath);

               if (foundNode != null)
               {
                   foundNode.Expand(); //loads child node
                   foundNode.Select();
                   // stored 1
                   // stored 1/1.1
                   findValueQuey = fValuePath;
               }               
           }
       }
       else
       {
           Response.Write("Node found : " + foundNode.Value);
       }
   }

Happy Node Finding!

Posted in ASP.NET, ASP.NET Controls | Tagged: | 1 Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 167 other followers