Wednesday, February 26, 2014

Parallel.For loop vs simple for loop

Here is the quick intro to Parallel.For loop to enable data parallelism and of-course benefits of same.

Parallel.For can be used when we can execute for loop iterations in parallel. Here source collection is partitioned and threads are assigned to execute actions.

Example:

Data:

string[] strArraly = this.textBox1.Text.Split(' ');  // textbox1 has a string of approx. 400 words.
int[] lenArray = new int[strArraly.Length];

For Loop:

     for (int i = 0; i < strArraly.Length; i++)
     {
         lenArray[i] = GetStringLengh(strArraly[i]);
     }

Parallel.For

     Parallel.For(0, strArraly.Length, i =>
     {
          lenArray[i] = GetStringLengh(strArraly[i]);
     }
     );

Time consuming Method:

       private int GetStringLengh(string str)
      {
          Thread.Sleep(100);
          return str.Length;
      }


Here if we check the time taken by for loop and Parallel.For loop, there is huge difference in both. For is taking around 38 seconds while Parallel For is taking only 6 second to execute all iterations. Thus Parallel.For can be very handy in some situations.


Tuesday, February 25, 2014

How to get OS version?

Here are steps to get OS version using C#.net code
Code line to get installed OS version string:
OperatingSystem OsInfo = Environment.OSVersion;

Steps to get Windows 95, Windows 98, Windows Me and Windows 98 Second Edition

case PlatformID.Win32Windows:
                    {
                        switch (osInfo.Version.Minor)
                        {
                            case 0:
                                {
                                    osName = "Windows 95";
                                    break;
                                }

                            case 10:
                                {
                                    if (osInfo.Version.Revision.ToString() == "2222A")
                                    {
                                        osName = "Windows 98 Second Edition";
                                    }
                                    else
                                    {
                                        osName = "Windows 98";
                                    }
                                    break;
                                }

                            case 90:
                                {
                                    osName = "Windows Me";
                                    break;
                                }
                        }
                        break;
                    }

For other versions, like XL, Windows7 and Windows8

case PlatformID.Win32NT:
                    {
                        switch (osInfo.Version.Major)
                        {
                            case 3:
                                {
                                    osName = "Windows NT 3.51";
                                    break;
                                }

                            case 4:
                                {
                                    osName = "Windows NT 4.0";
                                    break;
                                }

                            case 5:
                                {
                                    if (osInfo.Version.Minor == 0)
                                    {
                                        osName = "Windows 2000";
                                    }
                                    else if (osInfo.Version.Minor == 1)
                                    {
                                        osName = "Windows XP";
                                    }
                                    else if (osInfo.Version.Minor == 2)
                                    {
                                        osName = "Windows Server 2003";
                                    }
                                    break;
                                }

                            case 6:
                                if (osInfo.Version.Minor == 0)
                                {
                                    osName = "Windows Vista";           //6.0.x (Windows Vista)
                                }
                                else if (osInfo.Version.Minor == 1)
                                {
                                    osName = "Windows7";                //6.1.x (Windows 7 and above)
                                }
                                else
                                {
                                    osName = "Windows8";                //6.2.x (Windows 8 and above)
                                }
                                break;
                        }
                        break;

                    }

How to get installed .Net Framework version?

Here is the way to find out the installed version of .Net framework on your machine.

Manual Way
1.    Start run >> Regedit
2.  Go to “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\”
3.  Check keys under this. See image below for help.




C#.Net Code
Make sure to include following namespace in your class

       using Microsoft.Win32;


Method to get registry value

       public String GetVersion()
        {
            RegistryKey mobjSubRegistryKey;
            String strVersionDesc = "Installed: ";
            try
            {
                RegistryKey HKLM_Key = Registry.LocalMachine;
                mobjSubRegistryKey = HKLM_Key.OpenSubKey(
@"SOFTWARE\Microsoft\NET Framework Setup\NDP\");

                if (mobjSubRegistryKey == null)
                {
                    return "Not Installed";
                }

                String[] strarray = mobjSubRegistryKey.GetSubKeyNames();
               
                if (strarray == null)
                {
                    return "Not Installed";
                }

                foreach (String str in strarray)
                {
                    if (str == "v1.1.4322")
                    {
                        strVersionDesc += "[.Net Framework 1.1] ";
                    }
                    else if (str == "v2.0.50727")
                    {
                        strVersionDesc += "[.Net Framework 2.0] ";
                    }
                    else if (str == "v3.0")
                    {
                        strVersionDesc += "[.Net Framework 3.0] ";
                    }
                    else if (str == "v3.5")
                    {
                        strVersionDesc += "[.Net Framework 3.5] ";
                    }
                    else if (str == "v4")
                    {
                        strVersionDesc += "[.Net Framework 4.0] ";
                    }
                }

                return strVersionDesc;
            }
            catch(Exception ex)
            {
                return “An Error has occurred while capturing info”;
            }
        }