Saturday, July 27, 2013

How to read/write value from registry using C#.Net?

 In the winform some time we need to access the registry value or we need to modify the registry value to store or get some important information. For that in C# have simple option to handle the registry read/write using Microsoft.Win32.RegistryKey namespace. The below two methods will explain how to read key value from the local registry.


Save Value to Registory:
The WriteToRegistry method will write the value to registry based on Keyname. The Keyname must be exist before in the registry.


 public static void WriteToRegistry(string keyname, string keyvalue)
        {
            Microsoft.Win32.RegistryKey MainRootKey = null;
            Microsoft.Win32.RegistryKey subKey = null;                        
            try
            {
                MainRootKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE "true);
                if (MainRootKey!= null)
                {
                    subKey = rootKey.OpenSubKey("MyApplication"true);                  
                    if (subKey != null)
                    {
                        subKey.SetValue(keyname, keyvalue);
                        subKey.Close();
                        
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (subKey != null)
                    subKey.Close();
                subKey = null;
 
                if (MainRootKey != null)
                    MainRootKey.Close();
                MainRootKey = null;
            }
 
        }
 
 
How to read MyApplication Registry information using C#.net ?

 The GetFromRegistry Method will return the registry value based on the KeyName. The Keyname should be exist before in the registry.
 
 


private string GetFromRegistry(string keyname)
        {
            string strReturn = string.Empty;
            RegistryKey objReg = Registry.LocalMachine.OpenSubKey("SOFTWARE").OpenSubKey("MyApplication");
            if (objReg != null && objReg.GetValue(keyname) != null)
                strReturn = objReg.GetValue(keyname).ToString();
 
            return strReturn;
 
        }
 

Constraints in SQL

In SQL Constraints are mainly created in two ways. One is Column constrains and another one is table constraints.  The column constrains need to create in the each column. Table constraints need to create end of the CREATE query. A constraint is a property that we can assign to column in table. We can assign these Constraint properties during the time of creation of table (By Using CREATE TABLE statement) or during the time of changing the existing table structure (By Using ALTER TABLE statement) .By assigning constraint property on column we can prevent the users to enter inconsistent of data in columns.

Type of Constraints:

•    NOT NULL - Indicates that a column cannot store NULL value.
•    UNIQUE - Ensures that each rows for a column must have a unique value.
•    PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or  combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly.
•    FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table.
•    CHECK - Ensures that the value in a column meets a specific condition.
•    DEFAULT - Specifies a default value when specified none for this column.


Syntax of Constraints:

CREATE TABLE table_name
(
column_name1 data-type(size) constraint-name,
column_name2 data-type(size) constraint-name,
column_name3 data-type(size) constraint-name,
....
);

Wednesday, July 24, 2013

How to use threading-multithreading in c#

Simple threading in C#.Net

Threads: Threads are often called lightweight processes. However they are not process.es A Thread is a small set of executable instructions, which can be used to isolate a task from a process. Multiple threads are efficient way to obtain parallelism of hardware and give interactive user interaction to your applications.

C# supports parallel execution of code through multithreading. A thread is an independent execution path, able to run simultaneously with other threads.


In the C# the threading is very simple and more power full to improve the application performance. The below example I have created two threads. In the main thread I called the MyFirstThread() Function. That function is static function you can call inside your main program. Inside my main thread again I have created one more thread for the MySubThread function.  The sub thread method called MySubThread() function. So the total process was Thread and inside thread. It is one of the simple multi threading concept in the c#.net.


Threading Example:



using System; 
using System.Threading;
using System.IO;

namespace WorkTest
{
    public class Program
    {
        static void Main(string[] args)
        {
            ThreadStart tr = new ThreadStart(MyFirstThread);
            Thread MyThread = new Thread(tr);
            MyThread.Start();
            Console.ReadKey();
        }

        //Main Thread Function
        public static void MyFirstThread()
        {
            ThreadStart trSub = new ThreadStart(MySubThread);
            Thread MyThread;
            Console.WriteLine("Thread function started");
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(2000);
                Console.WriteLine("Thread count {0}", i);
                MyThread = new Thread(trSub);
                MyThread.Start();
            }
        }

        //Sub Thread function
        public static void MySubThread()
        {
            Console.WriteLine("Sub Thread started");
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("SubThread count :{0}", i);
                Thread.Sleep(4000);
            }
            Console.WriteLine("Sub Thread Ended");
        } 
    }
}



The Output like this:
Thread count: 0
SubThread count: 0
SubThread count :1
SubThread count :2
….
SubThread count :9
Thread count :1
SubThread count :0
SubThread count :1
...
SubThread count :9
Thread count :2
SubThread count :0

SubThread count :1
.....