<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8684601736910532112</id><updated>2012-02-16T05:27:17.147-08:00</updated><category term='C# Programming'/><title type='text'>Vinay Parekh's</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-708585340442203623</id><published>2011-01-28T10:43:00.000-08:00</published><updated>2011-01-28T10:45:34.545-08:00</updated><title type='text'>Open a Parent window from Child window</title><content type='html'>&lt;div&gt;&lt;br /&gt;&lt;/div&gt;window.opener.location.href="default.aspx";&lt;div&gt;&lt;br /&gt;&lt;div&gt;// To make it on top&lt;br /&gt;&lt;div&gt;window.opener.focus();&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-708585340442203623?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/708585340442203623/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/open-parent-window-from-child-window.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/708585340442203623'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/708585340442203623'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/open-parent-window-from-child-window.html' title='Open a Parent window from Child window'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-6286999484420918620</id><published>2011-01-28T09:50:00.000-08:00</published><updated>2011-01-28T09:53:02.153-08:00</updated><title type='text'>Everything you would want to know about manipulating windows can usually be found here</title><content type='html'>&lt;div&gt;Following link has good examples on various operations that can be done on window open in asp.net&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;a href="http://www.irt.org/script/window.htm"&gt;http://www.irt.org/script/window.htm&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-6286999484420918620?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/6286999484420918620/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/everything-you-would-want-to-know-about.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/6286999484420918620'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/6286999484420918620'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/everything-you-would-want-to-know-about.html' title='Everything you would want to know about manipulating windows can usually be found here'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-1075185109243760133</id><published>2011-01-20T09:45:00.000-08:00</published><updated>2011-01-20T09:50:41.136-08:00</updated><title type='text'>Steps for Resource File in Windows application</title><content type='html'>&lt;div&gt;&lt;span class="Apple-style-span" &gt;Get the Executing assembly&lt;/span&gt;&lt;/div&gt;&lt;div&gt; &lt;/div&gt;&lt;div&gt;Assembly executingAssembly = Assembly.GetExecutingAssembly()&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" &gt;Path of executing assembly&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;string assemblyLocation = executingAssembly.Location;&lt;/div&gt;&lt;div&gt;string dirName = Path.GetDirectoryName(assemblyLocation);&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" &gt;Get the path of UI assembly&lt;/span&gt;&lt;/div&gt;&lt;div&gt;Assembly UIassembly = Assembly.LoadFile(Path.Combine(dirName, "dllname"));&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" &gt;Initialize the Resource Manager&lt;/span&gt;&lt;/div&gt;&lt;div&gt;ResourceManager = new ResourceManager(@"resourcename", UIassembly);&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-1075185109243760133?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/1075185109243760133/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/steps-for-resource-file-in-windows.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/1075185109243760133'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/1075185109243760133'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/steps-for-resource-file-in-windows.html' title='Steps for Resource File in Windows application'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-6824819565245978355</id><published>2011-01-06T06:41:00.000-08:00</published><updated>2011-01-06T07:16:51.245-08:00</updated><title type='text'>Regular Expression for Date in dd/mm/yyyy</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: small; color: rgb(0, 0, 255); "&gt;^(((((0[1-9])|(1\d)|(2[0-8]))/((0[1-9])|(1[0-2])))|((31/((0[13578])|(1[02])))|((29|30)/((0[1,3-9])|(1[0-2])))))/((20[0-9][0-9]))|((((0[1-9])|(1\d)|(2[0-8]))/((0[1-9])|(1[0-2])))|((31/((0[13578])|(1[02])))|((29|30)/((0[1,3-9])|(1[0-2])))))/((19[0-9][0-9]))|(29/02/20(([02468][048])|([13579][26])))|(29/02/19(([02468][048])|([13579][26]))))$&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-6824819565245978355?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/6824819565245978355/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/regular-expression-for-date-in-ddmmyyyy.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/6824819565245978355'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/6824819565245978355'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2011/01/regular-expression-for-date-in-ddmmyyyy.html' title='Regular Expression for Date in dd/mm/yyyy'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-5061328839543683823</id><published>2009-12-16T05:44:00.000-08:00</published><updated>2009-12-16T06:00:50.259-08:00</updated><title type='text'>How to Export data from Excel to SQL using C#</title><content type='html'>The current example will show you how to export data from Excel to SQL database using C#. It requires OLEDB drivers need to be installed on your system(OLEDB drivers comes by default with office 2007 products) or you can download explicitly from &lt;a title="http://www.microsoft.com/downloads/details.aspx?FamilyID=" displaylang="en" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&amp;amp;displaylang=en&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;string strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0\"";&lt;br /&gt;&lt;br /&gt;//OLEDB connection for Excel&lt;br /&gt;&lt;br /&gt;OleDbConnection olConn = new OleDbConnection(strconn);&lt;br /&gt;OleDbDataReader orr = default(OleDbDataReader);&lt;br /&gt;olConn.Open();&lt;br /&gt;&lt;br /&gt;DataTable dt = olConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;OleDbCommand od = new OleDbCommand("SELECT * FROM ['" + ConfigurationManager.AppSettings["ExcelSheetName"].ToString() + "$']", olConn);&lt;br /&gt;if (od != null)&lt;br /&gt;{&lt;br /&gt;orr = od.ExecuteReader();&lt;br /&gt;//If Data Exists&lt;br /&gt;if (orr.HasRows)&lt;br /&gt;{&lt;br /&gt;string strconn1 = null;&lt;br /&gt;strconn1 = /* Provide connection string */&lt;br /&gt;SqlBulkCopy sqCopy = new SqlBulkCopy(strconn1);&lt;br /&gt;//Give the Destination table name&lt;br /&gt;sqCopy.DestinationTableName = "SampleTable";&lt;br /&gt;//Here the only condition is it takes the first character of first column of first row and creates column with it.&lt;br /&gt;Eg: if there is a word like "Finance" in the first column of first row it takes the column names as Finance and the second column as F2 and 3rd as F3 .....&lt;br /&gt;&lt;br /&gt;//So we need to create the destination table with the same column names and then do mapping&lt;br /&gt;sqCopy.ColumnMappings.Add("F2", "F2");&lt;br /&gt;sqCopy.ColumnMappings.Add("F3", "F3");&lt;br /&gt;sqCopy.ColumnMappings.Add("F4", "F4");&lt;br /&gt;sqCopy.ColumnMappings.Add("F5", "F5");&lt;br /&gt;sqCopy.ColumnMappings.Add("F6", "F6");&lt;br /&gt;sqCopy.ColumnMappings.Add("F7", "F7");&lt;br /&gt;sqCopy.ColumnMappings.Add("F8", "F8");&lt;br /&gt;sqCopy.ColumnMappings.Add("F9", "F9");&lt;br /&gt;sqCopy.ColumnMappings.Add("F10", "F10");&lt;br /&gt;//Then write to the server&lt;br /&gt;sqCopy.WriteToServer(orr);&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-5061328839543683823?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/5061328839543683823/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/12/how-to-export-data-from-excel-to-sql.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/5061328839543683823'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/5061328839543683823'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/12/how-to-export-data-from-excel-to-sql.html' title='How to Export data from Excel to SQL using C#'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-3834601787042263818</id><published>2009-12-06T22:36:00.000-08:00</published><updated>2009-12-06T22:50:22.998-08:00</updated><title type='text'>XMPP Gateway Works with OpenFire</title><content type='html'>Now the XMPPGateway works with Openfire.All you need to do is to install the latest patch available for XMPP Gateway at&lt;br /&gt;http://support.microsoft.com/?kbid=977187&lt;br /&gt;and configure openfire to support only TCPDIALBACK&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-3834601787042263818?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/3834601787042263818/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/12/xmppgateway-works-with-openfire.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/3834601787042263818'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/3834601787042263818'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/12/xmppgateway-works-with-openfire.html' title='XMPP Gateway Works with OpenFire'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-5545027569602270105</id><published>2009-07-14T03:01:00.000-07:00</published><updated>2009-07-14T05:29:56.322-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# Programming'/><title type='text'>Differences between Idictionary  and Hashtable</title><content type='html'>Both Idictionary and Hashtable are use to store data in a (Key,Value) &lt;key&gt;format.&lt;br /&gt;Here " Key " is any unique token which is used to identify its corresponding data or value uniquely, "Value" is the data which you want to store it can be any thing string, integer, float etc..&lt;br /&gt;Both(Idictionary and Hashtable) are derived from System.Collections Class. are used for&lt;br /&gt;&lt;br /&gt;1) Inserting Data&lt;br /&gt;2) Deleting data&lt;br /&gt;3) Modifying Data&lt;br /&gt;4) Searching for a particluar value&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;em&gt;Idictionary:&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;&lt;em&gt;&lt;u&gt;&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;In Idictionary we need to clearly define what type of value we want to store. let say we want to store both key and value as strings, here is the syntax&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;IDictionary &amp;lt string,string &amp;gt Name;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;Name= new Dictionary &amp;lt string,string &amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;&lt;/span&gt;&lt;br /&gt;So in the above we have created Name as Idictionary where Key and value both takes strings.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;em&gt;Hashtable:&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;&lt;em&gt;&lt;u&gt;&lt;/u&gt;&lt;/em&gt;&lt;br /&gt;In hashtable no need to define any signature i.e you can store any kind of values. Hashtable take every key and value as an object but not as the value you have entered means eventhough we have entered Key and value as strings Hashtable internally treats them as objects but not as strings.&lt;br /&gt;&lt;br /&gt;In the same hashtable you can store different values let say we have store Ist entry as (string, int) 2nd entry can be any thing let say (int, float). Since Hashtable treat every key or Value as an object .&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;Hashtable namelist;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;nameList= new Hashtable();&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;em&gt;Differences between Hashtable and Idictionary:&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;In Idictionary we need to explicitly define signature i.e what type of Key and value we want to store. Hashtable no need to define any structure or signature.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;In Idictionary once we have defined signature there is no chance of entering values other than the signature we have specified. where as within the same hashtable we can enter any type of key,value pair.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;In Hashtable we required Boxing and Unboxing to do any kind of manipulation with values. where as for Idictionary we don't require any kind of boxing and unboxing as we are already defined the signature.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Hashtable is useful if you don't know what all kind of values you might be storing in it. Idictionary is used if you know exactly what kind of (key , value) pair you will be using.Let say if you know that both Key and value are always strings then use Idictionary.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Hashtable is costly as every operation(inserting, modifying, deleting..) requires either Boxing or UnBoxing but where as Idictionary doesn't require any such things since we know the signature before hand.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-5545027569602270105?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/5545027569602270105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/why-we-need-to-use-idictionary-instead.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/5545027569602270105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/5545027569602270105'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/why-we-need-to-use-idictionary-instead.html' title='Differences between Idictionary  and Hashtable'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-4247737665240574379</id><published>2009-07-13T00:02:00.000-07:00</published><updated>2009-07-14T05:29:56.323-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# Programming'/><title type='text'>How to Run any Application at System start up</title><content type='html'>The below lines will create a registry entry at following path "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", this is the path from where windows will start all the applications at the system start up.&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;RegistryKey regKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;//Give the name of exe as first parameter . Here Sample.exe is the name of exe. The second parameter should be path from where it will start executing the exe's. Here&lt;br /&gt;"&lt;span style="color:#660000;"&gt;Application.ExecutablePath&lt;/span&gt;" will directly give the path of exe.&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;regKey .SetValue("Sample", Application.ExecutablePath.ToString());&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-4247737665240574379?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/4247737665240574379/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/how-to-run-any-application-at-system.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/4247737665240574379'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/4247737665240574379'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/how-to-run-any-application-at-system.html' title='How to Run any Application at System start up'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-3423072752779977046</id><published>2009-07-09T05:33:00.000-07:00</published><updated>2009-07-14T05:29:56.323-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# Programming'/><title type='text'>How to Read and write to a Xml document in c#</title><content type='html'>Following things i will be covering in this blog&lt;br /&gt;&lt;br /&gt;1) Creating a Xml file.&lt;br /&gt;2) Writing in to a Xml file .&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;u&gt;&lt;em&gt;Creating a Xml File :&lt;/em&gt;&lt;/u&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;To create a Xml file first we need to know the directory structure or the path where it has to create the Xml file. If the specified directory structure doesn't exist we need to create it.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;u&gt;Note&lt;/u&gt; : Make sure that you give only the path but not till the file Name.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;u&gt;Eg&lt;/u&gt;: If you want your xml file at&lt;/p&gt;&lt;p&gt;&lt;br /&gt;"C:\\Documents and Settings\\All Users\\Desktop\\Sample\\SampleConfig.xml"&lt;br /&gt;give the path till SampleFolder if you give full path(till the file name in this&lt;br /&gt;case Sampleconfig.xml) it will create the Folder by name SampleConfig.xml .&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;string xmlFilePath="C:\\Documents and Settings\\All Users\\Desktop\\Sample";&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;if (!Directory.Exists(xmlFilePath))&lt;br /&gt;Directory.CreateDirectory(xmlFilePath);&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;//Now append the filename to the path&lt;br /&gt;&lt;span style="color:#000099;"&gt;xmlFilePath+= "\\SampleConfig.xml";&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;So in the above code snippet it will create a folder by name Sample.&lt;/p&gt;&lt;p&gt;Once we know the path we can now create the xml file.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;XmlTextWriter txtWriter = new XmlTextWriter(filePath, null);&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;//This method is used to validates whether we are writing well-formed xml document(its optional)&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;txtWriter.WriteStartDocument(true);&lt;/span&gt;&lt;/p&gt;&lt;p&gt;//To create the xml file with indentation if we don't give this it keeps writing as a single line.Its optional.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;txtWriter.Formatting = Formatting.Indented;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;txtWriter.WriteStartElement("EmpDetails");&lt;br /&gt;txtWriter.WriteStartElement("EmpID");&lt;br /&gt;txtWriter.WriteEndElement();&lt;br /&gt;txtWriter.WriteStartElement("EmpName");&lt;br /&gt;txtWriter.WriteEndElement();&lt;br /&gt;txtWriter.WriteStartElement("Age");&lt;br /&gt;txtWriter.WriteEndElement();&lt;br /&gt;txtWriter.WriteEndElement();&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;It writes the following lines &lt;/p&gt;&lt;p&gt;&lt;span style="font-size:0;"&gt;&amp;ltEmpDetails&amp;gt&lt;/p&gt;&lt;/span&gt;&lt;p&gt;&amp;ltEmpID /&amp;gt&lt;/p&gt;&lt;p&gt;&amp;ltEmpName /&amp;gt&lt;/p&gt;&lt;p&gt;&amp;ltAge /&amp;gt&lt;/p&gt;&lt;p&gt;&amp;lt/EmpDetails&amp;gt"&lt;empdetails&gt;&lt;/p&gt;&lt;p&gt;&lt;empid&gt;&lt;/p&gt;&lt;p&gt;&lt;empname&gt;&lt;br /&gt;&lt;age&gt;&lt;/p&gt;&lt;span style="font-size:0;"&gt;&lt;p&gt;&lt;/empdetails&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;Here the important to point to note is that how we are creating parent node and child nodes. Here "EmpDetails" is the root node and EmpID,EmpName and Age are the child nodes. To create Parentnode close its corresponding EndElement(WriteEndElement()) at the end of its Child nodes. &lt;p&gt;From the above code snippet we can see WriteEndElement() of EmpDetails node is written at the end,after all the child nodes are completed but where as for other nodes we are writing at the very next line. We can create nested nodes too.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;We can create Xml nodes with default values, by using "WriteElementString" method of XmlTextWriter&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;txtWriter.WriteElementString("Age", "24");&lt;/span&gt;&lt;/p&gt;so here it will create a node with default value of age as 24.&lt;br /&gt;&lt;ul&gt;&lt;li&gt;We have till now created only the nodes but still they are not written in to the file, to do so we need to make use of "Flush" method.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;//This method writes above data in to Xml file&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;txtWriter.Flush();&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Its always good practice to close the XmlTextWriter if its no longer in use.&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;txtWriter.Close();&lt;/span&gt; &lt;/p&gt;&lt;u&gt;&lt;em&gt;Writing in to Xml:&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;To write in to xml, first we need to know the path from where we need to get the xml file. &lt;/li&gt;&lt;li&gt;Load the Xml in to XmlDocument.&lt;/li&gt;&lt;li&gt;Traverse through each node and check whether its the same node for which we need to give the value.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Step1 :&lt;/p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;XmlDocument xmlDoc = new XmlDocument();&lt;br /&gt;filePath+=&lt;a href="file://sampleconfig.xml/"&gt;file://sampleconfig.xml/&lt;/a&gt;;&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;&lt;span style="color:#000000;"&gt;Step2 :&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;&lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;br /&gt;xmlDoc.Load(filePath);&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000000;"&gt;Step3:&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;XmlNodeList xmlNodeList;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000099;"&gt;&lt;span style="color:#000000;"&gt;//This gives the list of child nodes&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#000099;"&gt;&lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;xmlNodeList = xmlDoc.DocumentElement.ChildNodes;&lt;/p&gt;&lt;p&gt;&lt;span style="color:#000000;"&gt;//Traverse through Each node and give the values&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#000000;"&gt;&lt;/span&gt;&lt;p&gt;&lt;br /&gt;foreach (XmlNode childNode in xmlNodeList)&lt;br /&gt;{&lt;br /&gt;if (childNode.NodeType == XmlNodeType.Element)&lt;br /&gt;{&lt;br /&gt;if (childNode.Name == "EmpID")&lt;br /&gt;childNode.InnerText = "12345";&lt;br /&gt;if (childNode.Name == "EmpName")&lt;br /&gt;childNode.InnerText = "OM";&lt;br /&gt;if (childNode.Name == "Age")&lt;br /&gt;childNode.InnerText = "24"; }&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#000000;"&gt;To save the configuration file use the save method of XmlDocument.&lt;/span&gt; &lt;p&gt;&lt;br /&gt;&lt;span style="color:#000099;"&gt;xmlDoc.Save(filePath);&lt;/span&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-3423072752779977046?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/3423072752779977046/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/how-to-read-and-write-to-xml-document.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/3423072752779977046'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/3423072752779977046'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/how-to-read-and-write-to-xml-document.html' title='How to Read and write to a Xml document in c#'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-2503001044630742716</id><published>2009-07-06T22:17:00.000-07:00</published><updated>2009-07-14T05:29:56.323-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# Programming'/><title type='text'>How to Read and write Asynchronously to Socket ?</title><content type='html'>C# provides built in methods to read and write data asynchronously in to the sockets. All this methods are non-blocking methods so we can still continue processing other steps without waiting for Asynchronous callback to complete since they are executed in a different thread.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;em&gt;Read Data Asynchronously :&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;socket.BeginReceive is a method defined in System.Net.Sockets to read data from a connected socket.&lt;br /&gt;Syntax:(one of the overloaded methods)&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;BeginReceive(byte [] buffer, int offset, int size, Socket flags, AsyncCallback, object state)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Here&lt;br /&gt;&lt;br /&gt;buffer = array of system byte to store data&lt;br /&gt;offset = position in buffer parameter from which it has to store data&lt;br /&gt;size = number of bytes it can received&lt;br /&gt;Socket flags = bitwise combination of socket flags&lt;br /&gt;AsyncCallback = a delegate which is invoked when we have received something from socket&lt;br /&gt;state = a user defined object which contians information about receive operation. This is also passed to EndReceive method.Generally it will have atleast the socket from which it has to read.&lt;br /&gt;&lt;br /&gt;Example :&lt;br /&gt;&lt;br /&gt;byte[] buffer = new byte[BufferSize];&lt;br /&gt;&lt;br /&gt;public class StateObject&lt;br /&gt;{&lt;br /&gt;public Socket workSocket = null; // Client socket.&lt;br /&gt;public byte[] buffer = new byte[BufferSize];&lt;br /&gt;}&lt;br /&gt;StateObject state=new StateObject();&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#660000;"&gt;Sock.BeginReceive(buffer, 0, 1024, 0, new AsyncCallback(ReadCallback), state);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So here buffer is the bytearray to store data starting from offest ,0 and to the maximun of 1024 bytes . ReadCallback is the delegate which is invoked when we received something on socket.&lt;br /&gt;&lt;br /&gt;The asynchronous BeginReceive operation must be completed by calling the &lt;span style="color:#3333ff;"&gt;EndReceive&lt;/span&gt; method.&lt;br /&gt;&lt;br /&gt;To cancel a pending BeginReceive, call the &lt;span style="color:#3333ff;"&gt;Close&lt;/span&gt; method.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public void ReadCallback(IAsyncResult ar)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;// Retrieve the state object and the handler socket from the asynchronous state object.&lt;br /&gt;state = (StateObject)ar.AsyncState;&lt;br /&gt;&lt;br /&gt;if (state != null)&lt;br /&gt;{&lt;br /&gt;// Read data from the client socket.&lt;br /&gt;int bytesRead = handler.EndReceive(ar);&lt;br /&gt;&lt;br /&gt;//Convert the bytedata to string&lt;br /&gt;string content = System.Text.ASCIIEncoding.ASCII.GetString(state.buffer, 0, bytesRead);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;u&gt;&lt;em&gt;Send data Asynchronously:&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;A Method called BeginSend is used to start sending data asynchronously on a socket. We can create a callback method that implements the AsyncCallback delegate and pass its name to the BeginSend method.&lt;br /&gt;&lt;br /&gt;The callback method make use of &lt;span style="color:#3333ff;"&gt;EndSend&lt;/span&gt; method used to send data.&lt;br /&gt;&lt;br /&gt;Syntax:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#3333ff;"&gt;BeginSend(byte [] buffer, int offset, int size, Socket flags, AsyncCallback, object state) ;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#660000;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#000000;"&gt;Example :&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Let sey we need to send string "HelloWorld".&lt;br /&gt;&lt;br /&gt;string str="HelloWorld";&lt;br /&gt;byte[] byteData = Encoding.UTF8.GetBytes(str);&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#660000;"&gt;sock.BeginSend(byteData, 0, byteData.Length, 0, new AsyncCallback(SendCallback), sock);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So here SendCallback is used as delegate to send data&lt;br /&gt;&lt;br /&gt;private void SendCallback(IAsyncResult ar)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;// Retrieve the socket from the state object.&lt;br /&gt;sock = (Socket)ar.AsyncState;&lt;br /&gt;&lt;br /&gt;// Complete sending the data to the remote device.&lt;br /&gt;int bytesSent = sock.EndSend(ar);&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-2503001044630742716?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/2503001044630742716/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/how-to-read-and-write-asynchronously-to.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/2503001044630742716'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/2503001044630742716'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/07/how-to-read-and-write-asynchronously-to.html' title='How to Read and write Asynchronously to Socket ?'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-1549352282438073894</id><published>2009-06-28T23:32:00.000-07:00</published><updated>2009-07-14T05:28:36.122-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# Programming'/><title type='text'>Threads Synchronization in C#</title><content type='html'>&lt;div&gt;&lt;font color="#660000"&gt;Following are the ways to handle Threads Synchronization in C# &lt;/font&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;font color="#660000"&gt;1)Using Lock Mechanism with keyword "Lock"&lt;br /&gt;2)Using Monitors&lt;br /&gt;3)Using Wait and Event Handles&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;u&gt;&lt;font color="#660000"&gt;&lt;em&gt;Thread Synchronization using Lock Key word:&lt;/em&gt; &lt;/font&gt;&lt;/u&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;Using "Lock" keyword allows only one thread to execute a piece of code while the other threads keep waiting until the lock is released. Lock takes object as an argument which is derived from System.object.&lt;br /&gt;&lt;br /&gt;Eg:&lt;br /&gt;&lt;font color="#ff6666"&gt;1)&lt;br /&gt;&lt;/font&gt;&lt;font color="#000000"&gt;class Synchronization&lt;br /&gt;{&lt;br /&gt;private object objLock = new object();&lt;br /&gt;void SampleMethod()&lt;br /&gt;{&lt;br /&gt;//Other threads keep waiting here until the other thread finishes the execution&lt;br /&gt;lock (objLock)&lt;br /&gt;{&lt;br /&gt;//Block which need to be accessed by only one thread at a time&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;2) If we have multiple instances of the same class, the above example will not work as each instance(with Mutliple threads ofcourse) might have acquired lock separately.&lt;br /&gt;To avoid this we need to declare the Object as "staic" and to avoid any misuse of the object(like some one assigning that object to something else) we can use "readonly" field.&lt;br /&gt;&lt;br /&gt;Eg: so the above example is changed as below&lt;br /&gt;&lt;br /&gt;&lt;font color="#000000"&gt;class Synchronization&lt;br /&gt;{&lt;br /&gt;private static readonly object objLock = new object();&lt;br /&gt;&lt;br /&gt;void SampleMethod()&lt;br /&gt;{&lt;br /&gt;//Other threads keep waiting here until the other thread finishes the execution&lt;br /&gt;lock (objLock)&lt;br /&gt;{&lt;br /&gt;//Block which need to be accessed by only one thread at a time&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;} &lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;/div&gt;&lt;font color="#660000"&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;u&gt;&lt;em&gt;Thread Synchronization using Monitor:&lt;/em&gt;&lt;/u&gt; &lt;/font&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;font color="#660000"&gt;Monitor is same as Lock ,infact Lock uses Monitor mechanism internally. Monitor class is predefined in C#. Net it has two methods Enter and Exit. Enter allows only one thread to enter the piece of code.&lt;br /&gt;&lt;br /&gt;&lt;font color="#000000"&gt;class Synchronization&lt;br /&gt;{&lt;br /&gt;object monitorObj = new object();&lt;br /&gt;public void SampleMethod()&lt;br /&gt;{&lt;br /&gt;System.Threading.Monitor.Enter(monitorObj);&lt;br /&gt;//Piece of code which needs to be Synchronized&lt;br /&gt;System.Threading.Monitor.Exit(monitorObj);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/font&gt;Lock are relatively more used than monitor as it is concise and easy to use. &lt;/font&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;&lt;u&gt;&lt;em&gt;Thread Synchronization using Events and Wait Handles&lt;/em&gt;&lt;/u&gt;: &lt;/font&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;font color="#660000"&gt;The above two Process (Lock and Monitor) is used for Locking a Piece of code under the method in which the lock is specified but it is not usefull when we want the Lock the thread accross series of methods. For this we need to use Events mechanism in C#. There are two states for events "signalled " and "un-signalled", by default the events are in un-signalled state.There are two built in events in C# .&lt;/font&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;1) AutoResetEvent&lt;/font&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;2) Manual ResetEvent&lt;/font&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;Thread is activated only if we change the state from un-signalled state to signalled state &lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;u&gt;&lt;em&gt;&lt;font color="#660000"&gt;(i)AutoResetEvent: &lt;/font&gt;&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;In AutoResetEvent, once execution is completed it automatically changes its state back to un-signalled . So once a new thread comes again at the same block it again starts executing (since it is in un-signalled state) .&lt;/font&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;u&gt;&lt;em&gt;&lt;font color="#660000"&gt;(ii)ManualResetEvent:&lt;/font&gt;&lt;/em&gt;&lt;/u&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;In ManualResetEvent ,once execution is completed it keeps the last state untill a explicit reset is done for that event. Let say event initially is in un-signalled state and one thread executes the process and set it to as Signalled state, so all the threads coming after this thread will never executes the same process as it is already set as signalled state by first thread until it is explicitly Reset.&lt;/font&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;font color="#660000"&gt;Example:&lt;/font&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;font color="#660000"&gt;&lt;font color="#000000"&gt;class AutoResetEventExample&lt;br /&gt;{&lt;br /&gt;private System.Threading.AutoResetEvent autoEvent;&lt;br /&gt;public void function1()&lt;br /&gt;{&lt;br /&gt;// One thread has entered&lt;br /&gt;//Do the processing&lt;br /&gt;//blocks the Thread untill it receives a signal&lt;br /&gt;autoEvent.WaitOne();&lt;br /&gt;//Starts processing once its receives the Signal&lt;br /&gt;}&lt;br /&gt;void Main()&lt;br /&gt;{&lt;br /&gt;//Creating a AutoResetEvent by initializing the event in un-signalled state(by passing false)&lt;br /&gt;autoEvent = new System.Threading.AutoResetEvent(false);&lt;br /&gt;//Creating a new thread&lt;br /&gt;Thread t = new Thread(function);&lt;br /&gt;t.Start();&lt;br /&gt;Thread.Sleep(1000);&lt;br /&gt;//Again signalling the worker thread as it is back to un-signalled state&lt;br /&gt;autoEvent.Set();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/font&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;&lt;/font&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;font color="#660000"&gt;&lt;/font&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-1549352282438073894?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/1549352282438073894/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/06/threads-synchronization-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/1549352282438073894'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/1549352282438073894'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/06/threads-synchronization-in-c.html' title='Threads Synchronization in C#'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8684601736910532112.post-255386758621982664</id><published>2009-06-25T05:28:00.000-07:00</published><updated>2009-07-14T05:29:56.328-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C# Programming'/><title type='text'>How to Create Singleton Class in C#</title><content type='html'>&lt;u&gt;Singleton Class:&lt;/u&gt;&lt;br /&gt;Singleton class means a class having one and only one instance at any point of time irrespective of the number of times it is defined or used.&lt;br /&gt;Steps to create Singleton class:&lt;br /&gt;1) Declare the instance of a class as static&lt;br /&gt;2) create a private constructor( so that nobody can create new object ) with body as empty&lt;br /&gt;3)Define a static method to create the instance of a class. If the instance already exists return the same instance and if it doesn't exist create a new one.&lt;br /&gt;Example:&lt;br /&gt;&lt;br /&gt;class Sample&lt;br /&gt;{&lt;br /&gt;//Creating a private static instance of class&lt;br /&gt;private static Sample inst = null;&lt;br /&gt;//create a lock object&lt;br /&gt;private static readonly object objLock = new object();&lt;br /&gt;//Creating a private constructor&lt;br /&gt;private Sample()&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;//Creating a Static method to create the instance of sample class&lt;br /&gt;public static Sample CreateInstance()&lt;br /&gt;{&lt;br /&gt;//Locking as in mutlithreading scenario there is a chance to create mutliple instances&lt;br /&gt;lock (objLock)&lt;br /&gt;{&lt;br /&gt;if (inst == null)&lt;br /&gt;{&lt;br /&gt;inst = new Sample();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;return inst;&lt;br /&gt;}&lt;br /&gt;}&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8684601736910532112-255386758621982664?l=vinayparekh.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinayparekh.blogspot.com/feeds/255386758621982664/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://vinayparekh.blogspot.com/2009/06/how-to-create-singleton-class-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/255386758621982664'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8684601736910532112/posts/default/255386758621982664'/><link rel='alternate' type='text/html' href='http://vinayparekh.blogspot.com/2009/06/how-to-create-singleton-class-in-c.html' title='How to Create Singleton Class in C#'/><author><name>Vin's</name><uri>http://www.blogger.com/profile/03943221874356843082</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='21' src='http://2.bp.blogspot.com/_mHFYP_-E2WQ/SlxiN9-0Y6I/AAAAAAAAAFY/a1jeJzWmf8M/S220/20-SM40627.jpg'/></author><thr:total>0</thr:total></entry></feed>
