<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>See Sharp</title>
	<atom:link href="http://csharphelp.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://csharphelp.wordpress.com</link>
	<description></description>
	<lastBuildDate>Wed, 25 Jun 2008 23:27:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='csharphelp.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>See Sharp</title>
		<link>http://csharphelp.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://csharphelp.wordpress.com/osd.xml" title="See Sharp" />
	<atom:link rel='hub' href='http://csharphelp.wordpress.com/?pushpress=hub'/>
		<item>
		<title>4. [3]</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/4-3/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/4-3/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:27:50 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=41</guid>
		<description><![CDATA[MyPacket.cs CSocketPacket.cs using System; using System.Collections.Generic; using System.Text; namespace MyPacket { public class CSocketPacket { public System.Net.Sockets.Socket thisSocket; public byte[] dataBuffer = new byte[100]; } } TcpSpecificPacket.cs using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net.Sockets; namespace MyPacket { public class TcpSpecificPacket { public TcpClient thisClient; public byte[] dataBuffer = new byte[100]; } }<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=41&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>MyPacket.cs</p>
<p>CSocketPacket.cs</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;</p>
<p>namespace MyPacket<br />
{<br />
public class CSocketPacket<br />
{<br />
public System.Net.Sockets.Socket thisSocket;<br />
public byte[] dataBuffer = new byte[100];<br />
}<br />
}</p>
<p>TcpSpecificPacket.cs</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
using System.Net.Sockets;</p>
<p>namespace MyPacket<br />
{<br />
public class TcpSpecificPacket<br />
{<br />
public TcpClient thisClient;<br />
public byte[] dataBuffer = new byte[100];<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/41/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/41/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=41&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/4-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>4. [2]</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/4-2/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/4-2/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:26:35 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=40</guid>
		<description><![CDATA[Async Client Designer namespace MyAsyncClient { partial class MyAsyncClientForm { /// &#60;summary&#62; /// Required designer variable. /// &#60;/summary&#62; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox txtPort; private System.Windows.Forms.Button cmdSend; private System.Windows.Forms.TextBox textBoxArrived; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.Button buttonConnect; private System.Windows.Forms.Button buttonClose; private System.Windows.Forms.TextBox textBoxSend; private System.Windows.Forms.TextBox txtIPAddr; private [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=40&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Async Client</p>
<p>Designer</p>
<p>namespace MyAsyncClient<br />
{<br />
partial class MyAsyncClientForm<br />
{<br />
/// &lt;summary&gt;<br />
/// Required designer variable.<br />
/// &lt;/summary&gt;<br />
private System.Windows.Forms.GroupBox groupBox1;<br />
private System.Windows.Forms.Label label1;<br />
private System.Windows.Forms.Label label2;<br />
private System.Windows.Forms.TextBox txtPort;<br />
private System.Windows.Forms.Button cmdSend;<br />
private System.Windows.Forms.TextBox textBoxArrived;<br />
private System.Windows.Forms.GroupBox groupBox2;<br />
private System.Windows.Forms.GroupBox groupBox3;<br />
private System.Windows.Forms.Button buttonConnect;<br />
private System.Windows.Forms.Button buttonClose;<br />
private System.Windows.Forms.TextBox textBoxSend;<br />
private System.Windows.Forms.TextBox txtIPAddr;<br />
private System.ComponentModel.IContainer components = null;</p>
<p>#region Windows Form Designer generated code</p>
<p>/// &lt;summary&gt;<br />
/// Required method for Designer support &#8211; do not modify<br />
/// the contents of this method with the code editor.<br />
/// &lt;/summary&gt;<br />
private void InitializeComponent()<br />
{<br />
this.groupBox1 = new System.Windows.Forms.GroupBox();<br />
this.buttonClose = new System.Windows.Forms.Button();<br />
this.buttonConnect = new System.Windows.Forms.Button();<br />
this.txtPort = new System.Windows.Forms.TextBox();<br />
this.label2 = new System.Windows.Forms.Label();<br />
this.txtIPAddr = new System.Windows.Forms.TextBox();<br />
this.label1 = new System.Windows.Forms.Label();<br />
this.textBoxSend = new System.Windows.Forms.TextBox();<br />
this.cmdSend = new System.Windows.Forms.Button();<br />
this.textBoxArrived = new System.Windows.Forms.TextBox();<br />
this.groupBox2 = new System.Windows.Forms.GroupBox();<br />
this.groupBox3 = new System.Windows.Forms.GroupBox();<br />
this.groupBox4 = new System.Windows.Forms.GroupBox();<br />
this.labelStatus = new System.Windows.Forms.Label();<br />
this.groupBox1.SuspendLayout();<br />
this.groupBox3.SuspendLayout();<br />
this.groupBox4.SuspendLayout();<br />
this.SuspendLayout();<br />
//<br />
// groupBox1<br />
//<br />
this.groupBox1.Controls.Add(this.buttonClose);<br />
this.groupBox1.Controls.Add(this.buttonConnect);<br />
this.groupBox1.Controls.Add(this.txtPort);<br />
this.groupBox1.Controls.Add(this.label2);<br />
this.groupBox1.Controls.Add(this.txtIPAddr);<br />
this.groupBox1.Controls.Add(this.label1);<br />
this.groupBox1.Location = new System.Drawing.Point(8, 8);<br />
this.groupBox1.Name = &#8220;groupBox1&#8243;;<br />
this.groupBox1.Size = new System.Drawing.Size(280, 77);<br />
this.groupBox1.TabIndex = 0;<br />
this.groupBox1.TabStop = false;<br />
this.groupBox1.Text = &#8220;Settings&#8221;;<br />
//<br />
// buttonClose<br />
//<br />
this.buttonClose.Enabled = false;<br />
this.buttonClose.Location = new System.Drawing.Point(160, 48);<br />
this.buttonClose.Name = &#8220;buttonClose&#8221;;<br />
this.buttonClose.Size = new System.Drawing.Size(96, 24);<br />
this.buttonClose.TabIndex = 5;<br />
this.buttonClose.Text = &#8220;Close&#8221;;<br />
this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);<br />
//<br />
// buttonConnect<br />
//<br />
this.buttonConnect.Location = new System.Drawing.Point(160, 16);<br />
this.buttonConnect.Name = &#8220;buttonConnect&#8221;;<br />
this.buttonConnect.Size = new System.Drawing.Size(96, 24);<br />
this.buttonConnect.TabIndex = 4;<br />
this.buttonConnect.Text = &#8220;Connect&#8221;;<br />
this.buttonConnect.Click += new System.EventHandler(this.cmdConnect_Click);<br />
//<br />
// txtPort<br />
//<br />
this.txtPort.Location = new System.Drawing.Point(72, 48);<br />
this.txtPort.Name = &#8220;txtPort&#8221;;<br />
this.txtPort.Size = new System.Drawing.Size(40, 20);<br />
this.txtPort.TabIndex = 3;<br />
this.txtPort.Text = &#8220;8000&#8243;;<br />
//<br />
// label2<br />
//<br />
this.label2.Location = new System.Drawing.Point(16, 48);<br />
this.label2.Name = &#8220;label2&#8243;;<br />
this.label2.Size = new System.Drawing.Size(32, 16);<br />
this.label2.TabIndex = 2;<br />
this.label2.Text = &#8220;Port:&#8221;;<br />
//<br />
// txtIPAddr<br />
//<br />
this.txtIPAddr.Location = new System.Drawing.Point(72, 24);<br />
this.txtIPAddr.Name = &#8220;txtIPAddr&#8221;;<br />
this.txtIPAddr.Size = new System.Drawing.Size(80, 20);<br />
this.txtIPAddr.TabIndex = 1;<br />
this.txtIPAddr.Text = &#8220;127.0.0.1&#8243;;<br />
//<br />
// label1<br />
//<br />
this.label1.Location = new System.Drawing.Point(16, 24);<br />
this.label1.Name = &#8220;label1&#8243;;<br />
this.label1.Size = new System.Drawing.Size(48, 16);<br />
this.label1.TabIndex = 0;<br />
this.label1.Text = &#8220;Host IP:&#8221;;<br />
//<br />
// textBoxSend<br />
//<br />
this.textBoxSend.Location = new System.Drawing.Point(16, 104);<br />
this.textBoxSend.Multiline = true;<br />
this.textBoxSend.Name = &#8220;textBoxSend&#8221;;<br />
this.textBoxSend.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;<br />
this.textBoxSend.Size = new System.Drawing.Size(264, 82);<br />
this.textBoxSend.TabIndex = 1;<br />
//<br />
// cmdSend<br />
//<br />
this.cmdSend.Location = new System.Drawing.Point(19, 104);<br />
this.cmdSend.Name = &#8220;cmdSend&#8221;;<br />
this.cmdSend.Size = new System.Drawing.Size(237, 24);<br />
this.cmdSend.TabIndex = 2;<br />
this.cmdSend.Text = &#8220;Send&#8221;;<br />
this.cmdSend.Click += new System.EventHandler(this.buttonSend_Click);<br />
//<br />
// textBoxArrived<br />
//<br />
this.textBoxArrived.Location = new System.Drawing.Point(16, 244);<br />
this.textBoxArrived.Multiline = true;<br />
this.textBoxArrived.Name = &#8220;textBoxArrived&#8221;;<br />
this.textBoxArrived.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;<br />
this.textBoxArrived.Size = new System.Drawing.Size(264, 72);<br />
this.textBoxArrived.TabIndex = 3;<br />
//<br />
// groupBox2<br />
//<br />
this.groupBox2.Location = new System.Drawing.Point(8, 228);<br />
this.groupBox2.Name = &#8220;groupBox2&#8243;;<br />
this.groupBox2.Size = new System.Drawing.Size(280, 96);<br />
this.groupBox2.TabIndex = 4;<br />
this.groupBox2.TabStop = false;<br />
this.groupBox2.Text = &#8220;Data Arrived&#8221;;<br />
//<br />
// groupBox3<br />
//<br />
this.groupBox3.Controls.Add(this.cmdSend);<br />
this.groupBox3.Location = new System.Drawing.Point(8, 88);<br />
this.groupBox3.Name = &#8220;groupBox3&#8243;;<br />
this.groupBox3.Size = new System.Drawing.Size(280, 134);<br />
this.groupBox3.TabIndex = 5;<br />
this.groupBox3.TabStop = false;<br />
this.groupBox3.Text = &#8220;Send Data&#8221;;<br />
//<br />
// groupBox4<br />
//<br />
this.groupBox4.Controls.Add(this.labelStatus);<br />
this.groupBox4.Location = new System.Drawing.Point(8, 330);<br />
this.groupBox4.Name = &#8220;groupBox4&#8243;;<br />
this.groupBox4.Size = new System.Drawing.Size(280, 42);<br />
this.groupBox4.TabIndex = 6;<br />
this.groupBox4.TabStop = false;<br />
this.groupBox4.Text = &#8220;Status&#8221;;<br />
//<br />
// labelStatus<br />
//<br />
this.labelStatus.AutoSize = true;<br />
this.labelStatus.ForeColor = System.Drawing.SystemColors.ActiveCaption;<br />
this.labelStatus.Location = new System.Drawing.Point(5, 16);<br />
this.labelStatus.Name = &#8220;labelStatus&#8221;;<br />
this.labelStatus.Size = new System.Drawing.Size(99, 13);<br />
this.labelStatus.TabIndex = 0;<br />
this.labelStatus.Text = &#8220;Status messages &#8230;&#8221;;<br />
//<br />
// MyAsyncClientForm<br />
//<br />
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);<br />
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br />
this.ClientSize = new System.Drawing.Size(294, 382);<br />
this.Controls.Add(this.groupBox4);<br />
this.Controls.Add(this.textBoxArrived);<br />
this.Controls.Add(this.textBoxSend);<br />
this.Controls.Add(this.groupBox1);<br />
this.Controls.Add(this.groupBox2);<br />
this.Controls.Add(this.groupBox3);<br />
this.Name = &#8220;MyAsyncClientForm&#8221;;<br />
this.Text = &#8220;MyAsyncClient&#8221;;<br />
this.groupBox1.ResumeLayout(false);<br />
this.groupBox1.PerformLayout();<br />
this.groupBox3.ResumeLayout(false);<br />
this.groupBox4.ResumeLayout(false);<br />
this.groupBox4.PerformLayout();<br />
this.ResumeLayout(false);<br />
this.PerformLayout();</p>
<p>}<br />
/// &lt;summary&gt;<br />
///<br />
/// &lt;/summary&gt;<br />
/// &lt;param name=&#8221;disposing&#8221;&gt;&lt;/param&gt;<br />
protected override void Dispose(bool disposing)<br />
{</p>
<p>if (disposing)<br />
{<br />
if (components != null)<br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose(disposing);<br />
}</p>
<p>#endregion</p>
<p>private System.Windows.Forms.GroupBox groupBox4;<br />
private System.Windows.Forms.Label labelStatus;<br />
}<br />
}</p>
<p>Form.cs</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Net.Sockets;<br />
using System.Net;<br />
using MyPacket;<br />
using System.IO;</p>
<p>namespace MyAsyncClient<br />
{<br />
public partial class MyAsyncClientForm : Form<br />
{<br />
private delegate void CrossThreadCallsDelegate(string status);<br />
private CrossThreadCallsDelegate crossThreadCall;<br />
private CrossThreadCallsDelegate crossThreadCallForStatus;</p>
<p>public TcpClient tcpClient;</p>
<p>public MyAsyncClientForm()<br />
{<br />
InitializeComponent();<br />
crossThreadCall = new CrossThreadCallsDelegate(WriteMessage);<br />
crossThreadCallForStatus = new CrossThreadCallsDelegate(ReportStatus);<br />
labelStatus.Text = &#8220;Client application started!&#8221;;<br />
}</p>
<p>private void WriteMessage(string message)<br />
{<br />
textBoxArrived.Text += message + Environment.NewLine;<br />
}</p>
<p>private void ReportStatus(string message)<br />
{<br />
labelStatus.Text = message + Environment.NewLine;<br />
}</p>
<p>private void EnableCommands(bool abEnableConnect)<br />
{<br />
buttonClose.Enabled = !abEnableConnect;<br />
buttonConnect.Enabled = abEnableConnect;<br />
}</p>
<p>private void cmdConnect_Click(object sender, System.EventArgs e)<br />
{</p>
<p>try<br />
{<br />
EnableCommands(true);<br />
// get the remote IP address&#8230;<br />
IPAddress ip = IPAddress.Parse(txtIPAddr.Text);<br />
int iPortNo = int.Parse(txtPort.Text);<br />
//create the end point<br />
IPEndPoint ipEnd = new IPEndPoint(ip, iPortNo);<br />
//create the socket instance&#8230;<br />
tcpClient = new TcpClient();<br />
//connect to the remote host&#8230;<br />
tcpClient.Connect(ipEnd);<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Client connected to &#8221; + ipEnd });<br />
EnableCommands(false);<br />
//watch for data ( asynchronously )&#8230;<br />
WaitForData();<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
EnableCommands(true);<br />
}<br />
}<br />
public void WaitForData()<br />
{<br />
try<br />
{<br />
TcpSpecificPacket theSocPkt = new TcpSpecificPacket();<br />
theSocPkt.thisClient = tcpClient;<br />
// now start to listen for any data&#8230;<br />
theSocPkt.thisClient.GetStream().BeginRead(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, OnDataReceived, theSocPkt);<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}</p>
<p>}</p>
<p>private void buttonSend_Click(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
byte[] byData = System.Text.Encoding.ASCII.GetBytes(textBoxSend.Text);<br />
tcpClient.GetStream().Write(byData, 0, byData.Length);<br />
textBoxSend.Text = &#8220;&#8221;;<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Sent &#8221; + byData.Length + &#8221; bytes of data.&#8221; });<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}</p>
<p>}</p>
<p>private void buttonClose_Click(object sender, System.EventArgs e)<br />
{<br />
if (tcpClient != null &amp;&amp; tcpClient.Connected)<br />
{<br />
tcpClient.Close();<br />
tcpClient = null;<br />
EnableCommands(true);<br />
}<br />
}</p>
<p>public void OnDataReceived(IAsyncResult asyn)<br />
{<br />
try<br />
{<br />
TcpSpecificPacket theSockId = (TcpSpecificPacket)asyn.AsyncState;<br />
int dataReceivedLength = theSockId.thisClient.GetStream().EndRead(asyn);<br />
if (dataReceivedLength != 0)<br />
{<br />
textBoxArrived.Invoke(crossThreadCall, new object[] { Encoding.UTF8.GetString(theSockId.dataBuffer, 0, dataReceivedLength) });<br />
//crossThreadCall(Encoding.UTF8.GetString(theSockId.dataBuffer, 0, dataReceivedLength));<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Received &#8221; + dataReceivedLength + &#8221; bytes of data from server.&#8221; });<br />
WaitForData();<br />
}<br />
} catch (IOException)<br />
{<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Server has been closed!&#8221; });<br />
}<br />
catch (ObjectDisposedException)<br />
{<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Socket has been closed!&#8221; });<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}<br />
}<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/40/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/40/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/40/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/40/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/40/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=40&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/4-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>4. [1]</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/4-1/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/4-1/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:25:18 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=39</guid>
		<description><![CDATA[Async Server Designer namespace MyAsyncServer { partial class MyAsyncServerForm { /// &#60;summary&#62; /// Required designer variable. /// &#60;/summary&#62; private System.Windows.Forms.Label labelPort; private System.Windows.Forms.TextBox txtPortNo; private System.Windows.Forms.Button buttonListen; private System.Windows.Forms.TextBox textBoxReceived; private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.TextBox textBoxSend; private System.Windows.Forms.Button buttonSend; private System.ComponentModel.IContainer components = null; #region Windows Form Designer generated [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=39&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Async Server</p>
<p>Designer</p>
<p>namespace MyAsyncServer<br />
{<br />
partial class MyAsyncServerForm<br />
{<br />
/// &lt;summary&gt;<br />
/// Required designer variable.<br />
/// &lt;/summary&gt;<br />
private System.Windows.Forms.Label labelPort;<br />
private System.Windows.Forms.TextBox txtPortNo;<br />
private System.Windows.Forms.Button buttonListen;<br />
private System.Windows.Forms.TextBox textBoxReceived;<br />
private System.Windows.Forms.GroupBox groupBox1;<br />
private System.Windows.Forms.GroupBox groupBox2;<br />
private System.Windows.Forms.GroupBox groupBox3;<br />
private System.Windows.Forms.TextBox textBoxSend;<br />
private System.Windows.Forms.Button buttonSend;<br />
private System.ComponentModel.IContainer components = null;</p>
<p>#region Windows Form Designer generated code</p>
<p>/// &lt;summary&gt;<br />
/// Required method for Designer support &#8211; do not modify<br />
/// the contents of this method with the code editor.<br />
/// &lt;/summary&gt;<br />
private void InitializeComponent()<br />
{<br />
this.groupBox1 = new System.Windows.Forms.GroupBox();<br />
this.buttonSet = new System.Windows.Forms.Button();<br />
this.textBoxSet = new System.Windows.Forms.TextBox();<br />
this.labelConn = new System.Windows.Forms.Label();<br />
this.buttonListen = new System.Windows.Forms.Button();<br />
this.txtPortNo = new System.Windows.Forms.TextBox();<br />
this.labelPort = new System.Windows.Forms.Label();<br />
this.textBoxReceived = new System.Windows.Forms.TextBox();<br />
this.groupBox2 = new System.Windows.Forms.GroupBox();<br />
this.buttonSend = new System.Windows.Forms.Button();<br />
this.textBoxSend = new System.Windows.Forms.TextBox();<br />
this.groupBox3 = new System.Windows.Forms.GroupBox();<br />
this.groupBox4 = new System.Windows.Forms.GroupBox();<br />
this.labelStatus = new System.Windows.Forms.Label();<br />
this.groupBox1.SuspendLayout();<br />
this.groupBox2.SuspendLayout();<br />
this.groupBox4.SuspendLayout();<br />
this.SuspendLayout();<br />
//<br />
// groupBox1<br />
//<br />
this.groupBox1.Controls.Add(this.buttonSet);<br />
this.groupBox1.Controls.Add(this.textBoxSet);<br />
this.groupBox1.Controls.Add(this.labelConn);<br />
this.groupBox1.Controls.Add(this.buttonListen);<br />
this.groupBox1.Controls.Add(this.txtPortNo);<br />
this.groupBox1.Controls.Add(this.labelPort);<br />
this.groupBox1.Location = new System.Drawing.Point(8, 16);<br />
this.groupBox1.Name = &#8220;groupBox1&#8243;;<br />
this.groupBox1.Size = new System.Drawing.Size(280, 75);<br />
this.groupBox1.TabIndex = 0;<br />
this.groupBox1.TabStop = false;<br />
this.groupBox1.Text = &#8220;Settings&#8221;;<br />
//<br />
// buttonSet<br />
//<br />
this.buttonSet.Location = new System.Drawing.Point(144, 42);<br />
this.buttonSet.Name = &#8220;buttonSet&#8221;;<br />
this.buttonSet.Size = new System.Drawing.Size(104, 23);<br />
this.buttonSet.TabIndex = 5;<br />
this.buttonSet.Text = &#8220;Set&#8221;;<br />
this.buttonSet.UseVisualStyleBackColor = true;<br />
this.buttonSet.Click += new System.EventHandler(this.buttonSet_Click);<br />
//<br />
// textBoxSet<br />
//<br />
this.textBoxSet.Location = new System.Drawing.Point(89, 42);<br />
this.textBoxSet.Name = &#8220;textBoxSet&#8221;;<br />
this.textBoxSet.Size = new System.Drawing.Size(47, 20);<br />
this.textBoxSet.TabIndex = 4;<br />
this.textBoxSet.Text = &#8220;10&#8243;;<br />
//<br />
// labelConn<br />
//<br />
this.labelConn.AutoSize = true;<br />
this.labelConn.Location = new System.Drawing.Point(16, 42);<br />
this.labelConn.Name = &#8220;labelConn&#8221;;<br />
this.labelConn.Size = new System.Drawing.Size(67, 13);<br />
this.labelConn.TabIndex = 3;<br />
this.labelConn.Text = &#8220;Client Count:&#8221;;<br />
//<br />
// buttonListen<br />
//<br />
this.buttonListen.Location = new System.Drawing.Point(144, 12);<br />
this.buttonListen.Name = &#8220;buttonListen&#8221;;<br />
this.buttonListen.Size = new System.Drawing.Size(104, 24);<br />
this.buttonListen.TabIndex = 2;<br />
this.buttonListen.Text = &#8220;Start Listening&#8221;;<br />
this.buttonListen.Click += new System.EventHandler(this.buttonListen_Click);<br />
//<br />
// txtPortNo<br />
//<br />
this.txtPortNo.Location = new System.Drawing.Point(89, 16);<br />
this.txtPortNo.Name = &#8220;txtPortNo&#8221;;<br />
this.txtPortNo.Size = new System.Drawing.Size(47, 20);<br />
this.txtPortNo.TabIndex = 1;<br />
this.txtPortNo.Text = &#8220;8000&#8243;;<br />
//<br />
// labelPort<br />
//<br />
this.labelPort.Location = new System.Drawing.Point(16, 16);<br />
this.labelPort.Name = &#8220;labelPort&#8221;;<br />
this.labelPort.Size = new System.Drawing.Size(72, 16);<br />
this.labelPort.TabIndex = 0;<br />
this.labelPort.Text = &#8220;Port Number:&#8221;;<br />
//<br />
// textBoxReceived<br />
//<br />
this.textBoxReceived.Location = new System.Drawing.Point(14, 281);<br />
this.textBoxReceived.Multiline = true;<br />
this.textBoxReceived.Name = &#8220;textBoxReceived&#8221;;<br />
this.textBoxReceived.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;<br />
this.textBoxReceived.Size = new System.Drawing.Size(266, 80);<br />
this.textBoxReceived.TabIndex = 1;<br />
//<br />
// groupBox2<br />
//<br />
this.groupBox2.Controls.Add(this.buttonSend);<br />
this.groupBox2.Controls.Add(this.textBoxSend);<br />
this.groupBox2.Location = new System.Drawing.Point(8, 97);<br />
this.groupBox2.Name = &#8220;groupBox2&#8243;;<br />
this.groupBox2.Size = new System.Drawing.Size(280, 155);<br />
this.groupBox2.TabIndex = 2;<br />
this.groupBox2.TabStop = false;<br />
this.groupBox2.Text = &#8220;Send Data&#8221;;<br />
//<br />
// buttonSend<br />
//<br />
this.buttonSend.Location = new System.Drawing.Point(16, 120);<br />
this.buttonSend.Name = &#8220;buttonSend&#8221;;<br />
this.buttonSend.Size = new System.Drawing.Size(232, 24);<br />
this.buttonSend.TabIndex = 1;<br />
this.buttonSend.Text = &#8220;Send&#8221;;<br />
this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);<br />
//<br />
// textBoxSend<br />
//<br />
this.textBoxSend.Cursor = System.Windows.Forms.Cursors.IBeam;<br />
this.textBoxSend.Location = new System.Drawing.Point(8, 16);<br />
this.textBoxSend.Multiline = true;<br />
this.textBoxSend.Name = &#8220;textBoxSend&#8221;;<br />
this.textBoxSend.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;<br />
this.textBoxSend.Size = new System.Drawing.Size(264, 96);<br />
this.textBoxSend.TabIndex = 0;<br />
//<br />
// groupBox3<br />
//<br />
this.groupBox3.Location = new System.Drawing.Point(8, 258);<br />
this.groupBox3.Name = &#8220;groupBox3&#8243;;<br />
this.groupBox3.Size = new System.Drawing.Size(280, 112);<br />
this.groupBox3.TabIndex = 3;<br />
this.groupBox3.TabStop = false;<br />
this.groupBox3.Text = &#8220;Data Received&#8221;;<br />
//<br />
// groupBox4<br />
//<br />
this.groupBox4.Controls.Add(this.labelStatus);<br />
this.groupBox4.Location = new System.Drawing.Point(8, 378);<br />
this.groupBox4.Name = &#8220;groupBox4&#8243;;<br />
this.groupBox4.Size = new System.Drawing.Size(280, 38);<br />
this.groupBox4.TabIndex = 4;<br />
this.groupBox4.TabStop = false;<br />
this.groupBox4.Text = &#8220;Satus&#8221;;<br />
//<br />
// labelStatus<br />
//<br />
this.labelStatus.AutoSize = true;<br />
this.labelStatus.Font = new System.Drawing.Font(&#8220;Microsoft Sans Serif&#8221;, 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));<br />
this.labelStatus.ForeColor = System.Drawing.SystemColors.ActiveCaption;<br />
this.labelStatus.Location = new System.Drawing.Point(6, 17);<br />
this.labelStatus.Name = &#8220;labelStatus&#8221;;<br />
this.labelStatus.Size = new System.Drawing.Size(99, 13);<br />
this.labelStatus.TabIndex = 0;<br />
this.labelStatus.Text = &#8220;Status messages &#8230;&#8221;;<br />
//<br />
// MyAsyncServerForm<br />
//<br />
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);<br />
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br />
this.ClientSize = new System.Drawing.Size(297, 428);<br />
this.Controls.Add(this.groupBox4);<br />
this.Controls.Add(this.groupBox2);<br />
this.Controls.Add(this.textBoxReceived);<br />
this.Controls.Add(this.groupBox1);<br />
this.Controls.Add(this.groupBox3);<br />
this.Name = &#8220;MyAsyncServerForm&#8221;;<br />
this.Text = &#8220;MyAsyncServer&#8221;;<br />
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.MyAsyncServerForm_FormClosed);<br />
this.groupBox1.ResumeLayout(false);<br />
this.groupBox1.PerformLayout();<br />
this.groupBox2.ResumeLayout(false);<br />
this.groupBox2.PerformLayout();<br />
this.groupBox4.ResumeLayout(false);<br />
this.groupBox4.PerformLayout();<br />
this.ResumeLayout(false);<br />
this.PerformLayout();</p>
<p>}</p>
<p>/// &lt;summary&gt;<br />
/// Clean up any resources being used.<br />
/// &lt;/summary&gt;<br />
/// &lt;param name=&#8221;disposing&#8221;&gt;true if managed resources should be disposed; otherwise, false.&lt;/param&gt;<br />
protected override void Dispose(bool disposing)<br />
{<br />
if (disposing &amp;&amp; (components != null))<br />
{<br />
components.Dispose();<br />
}<br />
base.Dispose(disposing);<br />
}</p>
<p>#endregion</p>
<p>private System.Windows.Forms.GroupBox groupBox4;<br />
private System.Windows.Forms.Label labelStatus;<br />
private System.Windows.Forms.Button buttonSet;<br />
private System.Windows.Forms.TextBox textBoxSet;<br />
private System.Windows.Forms.Label labelConn;<br />
}<br />
}</p>
<p>Form.cs</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Net.Sockets;<br />
using System.Net;<br />
using System.IO;<br />
using MyPacket;</p>
<p>namespace MyAsyncServer<br />
{<br />
public partial class MyAsyncServerForm : Form<br />
{<br />
private delegate void CrossThreadCallsDelegate(string status);<br />
private CrossThreadCallsDelegate crossThreadCall;<br />
private CrossThreadCallsDelegate crossThreadCallForStatus;<br />
private TcpListener tcpListener;<br />
// numarul maxim de conexiui acceptate, avem nevoie de el pentru ca nu putem apela beginAccept la infinit.<br />
private int clientCount = 0;</p>
<p>private List&lt;TcpClient&gt; clientList = new List&lt;TcpClient&gt;();</p>
<p>public MyAsyncServerForm()<br />
{<br />
InitializeComponent();<br />
buttonListen.Enabled = false;<br />
crossThreadCall = new CrossThreadCallsDelegate(WriteMessage);<br />
crossThreadCallForStatus = new CrossThreadCallsDelegate(ReportStatus);<br />
labelStatus.Text = &#8220;Application started!&#8221;;<br />
}</p>
<p>private void WriteMessage(string message)<br />
{<br />
/*<br />
ISynchronizeInvoke sync = this as ISynchronizeInvoke;<br />
if (sync.InvokeRequired)<br />
{<br />
sync.Invoke(crossThreadCall, new object[] { message });<br />
return;<br />
}*/<br />
textBoxReceived.Text += message + Environment.NewLine;<br />
}</p>
<p>private void ReportStatus(string message)<br />
{<br />
labelStatus.Text = message + Environment.NewLine;<br />
}</p>
<p>private void buttonListen_Click(object sender, EventArgs e)<br />
{<br />
try<br />
{<br />
//create the listening socket&#8230;<br />
int portNo = int.Parse(txtPortNo.Text);<br />
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, portNo);<br />
tcpListener = new TcpListener(ipLocal);<br />
//start listening&#8230;<br />
tcpListener.Start(10);<br />
// create the call back for any client connections&#8230;<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Server waiting for client&#8230;&#8221; });<br />
for (int i = 0; i &lt; clientCount; i++)<br />
{<br />
tcpListener.BeginAcceptTcpClient(OnClientConnect, null);<br />
}<br />
buttonListen.Enabled = false;<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}<br />
}</p>
<p>public void OnClientConnect(IAsyncResult asyn)<br />
{<br />
try<br />
{<br />
TcpClient tcpClient =  tcpListener.EndAcceptTcpClient(asyn);<br />
clientList.Add(tcpClient);<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Client connected from &#8221; + tcpClient.Client.RemoteEndPoint });<br />
WaitForData(tcpClient);<br />
}<br />
catch (ObjectDisposedException)<br />
{<br />
MessageBox.Show(&#8220;OnClientConnection: Socket has been closed!&#8221;);<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}<br />
}</p>
<p>public void WaitForData(TcpClient tcpClient)<br />
{<br />
try<br />
{<br />
TcpSpecificPacket theSocPkt = new TcpSpecificPacket();<br />
theSocPkt.thisClient = tcpClient;<br />
theSocPkt.thisClient.GetStream().BeginRead(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, OnDataReceived, theSocPkt);<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}</p>
<p>}</p>
<p>public void OnDataReceived(IAsyncResult asyn)<br />
{<br />
try<br />
{<br />
TcpSpecificPacket theSockId = (TcpSpecificPacket)asyn.AsyncState;</p>
<p>int dataReceivedLength = theSockId.thisClient.GetStream().EndRead(asyn);<br />
if (dataReceivedLength != 0)<br />
{<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { dataReceivedLength + &#8221; bytes received from client: &#8221; + theSockId.thisClient.Client.RemoteEndPoint });<br />
textBoxReceived.Invoke(crossThreadCall, new object[] { Encoding.UTF8.GetString(theSockId.dataBuffer, 0, dataReceivedLength) });<br />
WaitForData(theSockId.thisClient);<br />
}<br />
} catch(IOException) {<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Client has closed connection.&#8221; });<br />
}<br />
catch (ObjectDisposedException)<br />
{<br />
labelStatus.Invoke(crossThreadCallForStatus, new object[] { &#8220;Client socket has been closed.&#8221; });<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}<br />
}</p>
<p>private void buttonSend_Click(object sender, System.EventArgs e)<br />
{<br />
try<br />
{<br />
byte[] byData = Encoding.UTF8.GetBytes(textBoxSend.Text);<br />
foreach (TcpClient cl in clientList)<br />
{<br />
if (cl != null &amp;&amp; cl.Connected) {<br />
cl.GetStream().Write(byData, 0, byData.Length);<br />
}<br />
}</p>
<p>textBoxSend.Text = &#8220;&#8221;;<br />
}<br />
catch (SocketException se)<br />
{<br />
MessageBox.Show(se.Message);<br />
}<br />
}</p>
<p>private void buttonSet_Click(object sender, EventArgs e)<br />
{<br />
try<br />
{<br />
clientCount = int.Parse(textBoxSet.Text);<br />
buttonListen.Enabled = true;<br />
buttonSet.Enabled = false;<br />
}<br />
catch {<br />
MessageBox.Show(&#8220;Format necorespunzator!&#8221;);<br />
}<br />
}</p>
<p>private void MyAsyncServerForm_FormClosed(object sender, FormClosedEventArgs e)<br />
{<br />
if (tcpListener != null)<br />
{<br />
// ma asigur ca inchid toate conexiunile<br />
foreach (TcpClient cl in clientList)<br />
{<br />
cl.Close();<br />
}<br />
tcpListener.Stop();<br />
}<br />
}</p>
<p>private void MyAsyncServerForm_Load(object sender, EventArgs e)<br />
{</p>
<p>}<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/39/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/39/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/39/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/39/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/39/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=39&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/4-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>Mutex</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/mutex/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/mutex/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:21:51 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=38</guid>
		<description><![CDATA[LockMutex.cs using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Threading; namespace First { public class LockMutex { FileStream resource = null; public LockMutex(String pathToFile) { resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite); } public void Read() { //lock (resource) &#8211; am comentat liniile astea pentru a permite cititorilor accesul in paralel // { int thId [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=38&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>LockMutex.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
using System.Threading;</p>
<p>namespace First<br />
{<br />
public class LockMutex<br />
{<br />
FileStream resource = null;</p>
<p>public LockMutex(String pathToFile)<br />
{<br />
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);<br />
}</p>
<p>public void Read() {<br />
//lock (resource) &#8211; am comentat liniile astea pentru a permite cititorilor accesul in paralel<br />
// {<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.Begin);<br />
StreamReader sr = new StreamReader(resource);<br />
String s = sr.ReadToEnd();<br />
Console.WriteLine(thId.ToString() + &#8221; read : &#8221; + s);<br />
// }<br />
}</p>
<p>public void Write() {<br />
lock (resource)<br />
{<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.End);<br />
StreamWriter sw = new StreamWriter(resource);<br />
String s = thId + &#8221; :: &#8221; + System.DateTime.Now.ToString();<br />
sw.WriteLine(s);<br />
Console.WriteLine(thId.ToString() + &#8221; wrote : &#8221; + s);<br />
sw.Flush();<br />
}<br />
}</p>
<p>// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc<br />
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila<br />
public void Close()<br />
{<br />
resource.Close();<br />
}</p>
<p>/* destructor care asigura eliberarea resursei &#8211; nu e nevoie de el aici<br />
~LockMutex() {<br />
resource.Close();<br />
}*/</p>
<p>}<br />
}<br />
<strong>MonitorMutex.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using System.IO;</p>
<p>namespace First<br />
{<br />
class MonitorMutex<br />
{<br />
static object synObj = new object();<br />
FileStream resource = null;</p>
<p>public MonitorMutex(String pathToFile)<br />
{<br />
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);<br />
}</p>
<p>public void Read()<br />
{<br />
// Monitor.Enter(synObj); &#8211; am comentat liniile astea pentru a permite cititorilor accesul in paralel<br />
//  try<br />
//  {<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.Begin);<br />
StreamReader sr = new StreamReader(resource);<br />
String s = sr.ReadToEnd();<br />
Console.WriteLine(thId.ToString() + &#8221; read : &#8221; + s);<br />
//  }<br />
//  catch (Exception e){<br />
//     Console.WriteLine(e.Message);<br />
// }<br />
//      Monitor.Exit(synObj);</p>
<p>}</p>
<p>public void Write()<br />
{<br />
Monitor.Enter(synObj);<br />
try<br />
{<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.End);<br />
StreamWriter sw = new StreamWriter(resource);<br />
String s = thId + &#8221; :: &#8221; + System.DateTime.Now.ToString();<br />
sw.WriteLine(s);<br />
Console.WriteLine(thId.ToString() + &#8221; wrote : &#8221; + s);<br />
sw.Flush();<br />
}<br />
catch (Exception e)<br />
{<br />
Console.WriteLine(e.Message);<br />
}<br />
Monitor.Exit(synObj);<br />
}</p>
<p>// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc<br />
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila<br />
public void Close() {<br />
resource.Close();<br />
}</p>
<p>/*destructor care asigura eliberarea resursei &#8211; nu e nevoie de el aici<br />
~MonitorMutex() {<br />
resource.Close();<br />
}*/<br />
}<br />
}<br />
<strong>MutexMutex.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
using System.Threading;</p>
<p>namespace First<br />
{<br />
class MutexMutex<br />
{<br />
FileStream resource = null;<br />
public Mutex fileMutex;</p>
<p>public MutexMutex(String pathToFile)<br />
{<br />
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);<br />
fileMutex = new Mutex();<br />
}</p>
<p>public void Read()<br />
{<br />
//fileMutex.WaitOne(); &#8211; am comentat liniile astea pentru a permite cititorilor accesul in paralel<br />
//try<br />
//{<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.Begin);<br />
StreamReader sr = new StreamReader(resource);<br />
String s = sr.ReadToEnd();<br />
Console.WriteLine(thId.ToString() + &#8221; read : &#8221; + s);<br />
//} catch (Exception e) {<br />
//   Console.WriteLine(&#8220;Exception happened: &#8221; + e.Message);<br />
// }<br />
// fileMutex.ReleaseMutex();<br />
}</p>
<p>public void Write()<br />
{<br />
fileMutex.WaitOne();<br />
try {<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.End);<br />
StreamWriter sw = new StreamWriter(resource);<br />
String s = thId + &#8221; :: &#8221; + System.DateTime.Now.ToString();<br />
sw.WriteLine(s);<br />
Console.WriteLine(thId.ToString() + &#8221; wrote : &#8221; + s);<br />
sw.Flush();<br />
} catch (Exception e) {<br />
Console.WriteLine(&#8220;Exception happened: &#8221; + e.Message);<br />
}<br />
fileMutex.ReleaseMutex();<br />
}</p>
<p>// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc<br />
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila<br />
public void Close() {<br />
fileMutex.Close();<br />
resource.Close();<br />
}</p>
<p>/*destructor care asigura eliberarea resursei &#8211; nu e nevoie de el aici<br />
~MutexMutex() {<br />
fileMutex.Close();<br />
resource.Close();<br />
}*/<br />
}<br />
}<br />
<strong>Program.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;</p>
<p>namespace First<br />
{<br />
class Program<br />
{<br />
// metoda  ce poae fi folosita pentru a vehicula date de dimensiuni mari<br />
public static String getSring(int id) {<br />
StringBuilder sb = new StringBuilder();<br />
for (int i = 0; i &lt; 2000; i++)<br />
sb.AppendLine(id + &#8220;wrote: &#8221; + System.DateTime.Now);<br />
return sb.ToString();<br />
}</p>
<p>static void Main(string[] args)<br />
{<br />
//s-a dat accesul cititorilorx in paralel la resursa<br />
Thread[] ta = new Thread[10];<br />
LockMutex mx = null;<br />
MonitorMutex mm = null;<br />
MutexMutex mxmx = null;<br />
SemaphoreMutex sm = null;<br />
ReaderWriterLockMutex rwm = null;<br />
mx = new LockMutex(&#8220;test.txt&#8221;);<br />
Thread firstWriter = new Thread(mx.Write);<br />
firstWriter.Start();<br />
firstWriter.Join();<br />
for (int i = 0; i &lt; 10; ++i)<br />
{<br />
int opt = new Random().Next(2);<br />
ta[i] = opt == 0 ? new Thread(mx.Read) : new Thread(mx.Write);<br />
ta[i].Start();<br />
}<br />
Console.WriteLine(&#8220;\n&gt;&gt;&gt; Press any key to finish lock example and release resources&#8230;&lt;&lt;&lt;&#8221;);<br />
Console.ReadKey();<br />
Console.WriteLine(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8221;);<br />
mx.Close();</p>
<p>mm = new MonitorMutex(&#8220;test.txt&#8221;);<br />
firstWriter = new Thread(mm.Write);<br />
firstWriter.Start();<br />
firstWriter.Join();<br />
for (int i = 0; i &lt; 10; ++i)<br />
{<br />
int opt = new Random().Next(2);<br />
ta[i] = opt == 0 ? new Thread(mm.Read) : new Thread(mm.Write);<br />
ta[i].Start();<br />
}<br />
Console.WriteLine(&#8220;\n&gt;&gt;&gt; Press any key to finish Monitor example and release resources&#8230;&lt;&lt;&lt;&#8221;);<br />
Console.ReadKey();<br />
Console.WriteLine(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8221;);<br />
mm.Close();</p>
<p>mxmx = new MutexMutex(&#8220;test.txt&#8221;);<br />
firstWriter = new Thread(mxmx.Write);<br />
firstWriter.Start();<br />
firstWriter.Join();<br />
for (int i = 0; i &lt; 10; ++i)<br />
{<br />
int opt = new Random().Next(2);<br />
ta[i] = opt == 0 ? new Thread(mxmx.Read) : new Thread(mxmx.Write);<br />
ta[i].Start();<br />
}<br />
Console.WriteLine(&#8220;\n&gt;&gt;&gt; Press any key to finish Mutex example and release resources&#8230;&lt;&lt;&lt;&#8221;);<br />
Console.ReadKey();<br />
Console.WriteLine(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8221;);<br />
mxmx.Close();</p>
<p>sm = new SemaphoreMutex(&#8220;test.txt&#8221;);<br />
firstWriter = new Thread(sm.Write);<br />
firstWriter.Start();<br />
firstWriter.Join();<br />
for (int i = 0; i &lt; 10; ++i)<br />
{<br />
int opt = new Random().Next(2);<br />
ta[i] = opt == 0 ? new Thread(sm.Read) : new Thread(sm.Write);<br />
ta[i].Start();<br />
}<br />
Console.WriteLine(&#8220;\n&gt;&gt;&gt; Press any key to finish Semaphore example and release resources&#8230;&lt;&lt;&lt;&#8221;);<br />
Console.ReadKey();<br />
Console.WriteLine(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8221;);<br />
sm.Close();</p>
<p>rwm = new ReaderWriterLockMutex(&#8220;test.txt&#8221;);<br />
firstWriter = new Thread(new ParameterizedThreadStart(rwm.Write));<br />
firstWriter.Start(ReaderWriterLockMutex.writerTimeouts);<br />
firstWriter.Join();<br />
for (int i = 0; i &lt; 10; ++i)<br />
{<br />
int opt = new Random().Next(2);<br />
if (opt == 0)<br />
{<br />
ta[i] = new Thread(new ParameterizedThreadStart(rwm.Read));<br />
ta[i].Start(ReaderWriterLockMutex.readerTimeouts);<br />
}<br />
else {<br />
ta[i] = new Thread(new ParameterizedThreadStart(rwm.Write));<br />
ta[i].Start(ReaderWriterLockMutex.writerTimeouts);<br />
}<br />
}<br />
Console.WriteLine(&#8220;\n&gt;&gt;&gt; Press any key to finish ReaderWriterLock example and release resources&#8230;&lt;&lt;&lt;&#8221;);<br />
Console.ReadKey();<br />
Console.WriteLine(&#8220;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8221;);<br />
rwm.Close();</p>
<p>Console.WriteLine(&#8220;\n&gt;&gt;&gt; Press any key to exit&#8230;&lt;&lt;&lt;&#8221;);<br />
Console.ReadKey();<br />
}<br />
}<br />
}<br />
<strong>ReaderWriterLockMutex.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
using System.Threading;</p>
<p>namespace First<br />
{<br />
class ReaderWriterLockMutex<br />
{<br />
FileStream resource = null;<br />
ReaderWriterLock readerWriterLock = null;<br />
public static int readerTimeouts = 0;<br />
public static int writerTimeouts = 0;<br />
int reads = 0;<br />
int writes = 0;</p>
<p>public ReaderWriterLockMutex(String pathToFile)<br />
{<br />
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);<br />
readerWriterLock  = new ReaderWriterLock();<br />
}</p>
<p>public void Read(object obj)<br />
{<br />
int timeOut = (int)obj;<br />
try<br />
{<br />
readerWriterLock.AcquireReaderLock(timeOut);<br />
try<br />
{<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.Begin);<br />
StreamReader sr = new StreamReader(resource);<br />
String s = sr.ReadToEnd();<br />
Console.WriteLine(thId.ToString() + &#8221; read : &#8221; + s);<br />
Interlocked.Increment(ref reads);<br />
}<br />
finally<br />
{<br />
readerWriterLock.ReleaseReaderLock();<br />
}<br />
}<br />
catch (ApplicationException)<br />
{<br />
Interlocked.Increment(ref  readerTimeouts);<br />
}<br />
}</p>
<p>public void Write(object obj)<br />
{<br />
int timeOut = (int)obj;<br />
try<br />
{<br />
readerWriterLock.AcquireWriterLock(timeOut);<br />
try<br />
{<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.End);<br />
StreamWriter sw = new StreamWriter(resource);<br />
String s = thId + &#8221; :: &#8221; + System.DateTime.Now.ToString();<br />
sw.WriteLine(s);<br />
Console.WriteLine(thId.ToString() + &#8221; wrote : &#8221; + s);<br />
sw.Flush();<br />
Interlocked.Increment(ref  writes);<br />
}<br />
finally<br />
{<br />
readerWriterLock.ReleaseWriterLock();<br />
}<br />
}<br />
catch (ApplicationException)<br />
{<br />
Interlocked.Increment(ref writerTimeouts);<br />
}<br />
}</p>
<p>// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc<br />
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila<br />
public void Close()<br />
{<br />
resource.Close();<br />
}</p>
<p>/*destructor care asigura eliberarea resursei &#8211; nu e nevoie de el aici<br />
~ReaderWriterLockMutex() {<br />
resource.Close();<br />
}*/</p>
<p>}<br />
}<br />
<strong>SemaphoreMutex.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
using System.Threading;</p>
<p>namespace First<br />
{<br />
class SemaphoreMutex<br />
{<br />
FileStream resource = null;<br />
Semaphore semaphore = null;</p>
<p>public SemaphoreMutex(String pathToFile)<br />
{<br />
resource = new FileStream(pathToFile, FileMode.Create, FileAccess.ReadWrite);<br />
semaphore = new Semaphore(10, 10);<br />
}</p>
<p>public void Read()<br />
{<br />
//semaphore.WaitOne(); &#8211; am comentat liniile astea pentru a permite cititorilor accesul in paralel<br />
//try<br />
// {<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.Begin);<br />
StreamReader sr = new StreamReader(resource);<br />
String s = sr.ReadToEnd();<br />
Console.WriteLine(thId.ToString() + &#8221; read : &#8221; + s);<br />
// } catch (Exception e) {<br />
//   Console.WriteLine(&#8220;Exception happened: &#8221; + e.Message);<br />
//}<br />
//semaphore.Release();<br />
}</p>
<p>public void Write()<br />
{<br />
semaphore.WaitOne();<br />
try<br />
{<br />
int thId = Thread.CurrentThread.ManagedThreadId;<br />
resource.Seek(0, SeekOrigin.End);<br />
StreamWriter sw = new StreamWriter(resource);<br />
String s = thId + &#8221; :: &#8221; + System.DateTime.Now.ToString();<br />
sw.WriteLine(s);<br />
Console.WriteLine(thId.ToString() + &#8221; wrote : &#8221; + s);<br />
sw.Flush();<br />
}<br />
catch (Exception e)<br />
{<br />
Console.WriteLine(&#8220;Exception happened: &#8221; + e.Message);<br />
}<br />
semaphore.Release();<br />
}</p>
<p>// am avut nevoie de metoda asta din cauza switch-ului, in momentul in care folosesc<br />
// cealalta clasa ptr managementul resursei trebuie sa fiu sigura ca aceasta este disponibila<br />
public void Close()<br />
{<br />
semaphore.Close();<br />
resource.Close();<br />
}</p>
<p>/*destructor care asigura eliberarea resursei &#8211; nu e nevoie de el aici<br />
~SemaphoreMutex() {<br />
semaphore.Close();<br />
resource.Close();<br />
}*/<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/38/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/38/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/38/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/38/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/38/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=38&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/mutex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>Reader/Writer</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/readerwriter/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/readerwriter/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:17:01 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=37</guid>
		<description><![CDATA[3 projects: Main project, Reader, Writer. MainProject, Program.cs using System; using System.Collections.Generic; using System.Text; using System.IO; namespace MainProject { class Program { static void Main(string[] args) { FileStream fs = new FileStream(&#8220;test.txt&#8221;, FileMode.Create); fs.Close(); Console.WriteLine(&#8220;Current context :&#8221; + AppDomain.CurrentDomain.Id); AppDomain dw = AppDomain.CreateDomain(&#8220;Writers domain&#8221;); AppDomain dr = AppDomain.CreateDomain(&#8220;Readers domain&#8221;); for (int i = 0; i [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=37&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>3 projects: Main project, Reader, Writer.</p>
<p><strong>MainProject, Program.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;</p>
<p>namespace MainProject<br />
{<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
FileStream fs = new FileStream(&#8220;test.txt&#8221;, FileMode.Create);<br />
fs.Close();</p>
<p>Console.WriteLine(&#8220;Current context :&#8221; + AppDomain.CurrentDomain.Id);</p>
<p>AppDomain dw = AppDomain.CreateDomain(&#8220;Writers domain&#8221;);<br />
AppDomain dr = AppDomain.CreateDomain(&#8220;Readers domain&#8221;);</p>
<p>for (int i = 0; i &lt; 20; i++)<br />
{<br />
int opt = new Random().Next(2);<br />
if (opt == 0)<br />
{<br />
dw.ExecuteAssembly(@&#8221;..\..\..\Reader\bin\Debug\Reader.exe&#8221;);<br />
}<br />
else {<br />
dr.ExecuteAssembly(@&#8221;..\..\..\Writer\bin\Debug\Writer.exe&#8221;);<br />
}<br />
}</p>
<p>Console.WriteLine(&#8220;&gt;&gt;&gt; Press any key to exit&#8230; &lt;&lt;&lt;&#8221;);<br />
Console.ReadKey();<br />
}<br />
}<br />
}</p>
<p><strong>Reader, Reader.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using System.IO;</p>
<p>namespace Reader<br />
{<br />
class Reader<br />
{<br />
static Mutex localMutex = null;<br />
const string mutexName = &#8220;LocalMutex&#8221;;<br />
static string path = @&#8221;..\..\..\MainProject\bin\Debug\test.txt&#8221;;</p>
<p>static void Main(string[] args)<br />
{<br />
//Create the mutex****************************************<br />
try<br />
{<br />
localMutex = Mutex.OpenExisting(mutexName);<br />
}<br />
catch (WaitHandleCannotBeOpenedException)<br />
{<br />
Console.WriteLine(&#8220;Mutex is Null!&#8221;);<br />
}<br />
if (localMutex == null)<br />
{<br />
localMutex = new Mutex(false, mutexName);<br />
}</p>
<p>new Thread(ReadFromFile).Start();<br />
}</p>
<p>static void ReadFromFile()<br />
{<br />
if (localMutex.WaitOne(100000, false))<br />
{<br />
StreamReader sr = null;<br />
try<br />
{<br />
sr = new StreamReader(path);<br />
sr.BaseStream.Seek(0, SeekOrigin.Begin);<br />
Console.WriteLine(&#8220;domain no.:&#8221; + AppDomain.CurrentDomain.Id + &#8220;, Thread:&#8221; + Thread.CurrentThread.ManagedThreadId + &#8221; read: &#8221; + sr.ReadToEnd() );<br />
Thread.Sleep(200); // delay necesar observarii apelurilor in paralel<br />
}<br />
catch (IOException e)<br />
{<br />
Console.WriteLine(e.Message);<br />
}<br />
finally<br />
{<br />
sr.Close();<br />
localMutex.ReleaseMutex();<br />
}<br />
}<br />
}<br />
}<br />
}</p>
<p><strong>Writer, Writer.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using System.IO;</p>
<p>namespace Writer<br />
{<br />
class Writer<br />
{<br />
static Mutex localMutex;<br />
const string MutexName = &#8220;LocalMutex&#8221;;<br />
static string path = @&#8221;..\..\..\MainProject\bin\Debug\test.txt&#8221;;</p>
<p>static void Main(string[] args)<br />
{<br />
localMutex = null;<br />
try<br />
{<br />
localMutex = Mutex.OpenExisting(MutexName);<br />
}<br />
catch (WaitHandleCannotBeOpenedException)<br />
{<br />
Console.WriteLine(&#8220;Mutex is Null!&#8221;);<br />
}<br />
if (localMutex == null)<br />
{<br />
localMutex = new Mutex(false, MutexName);<br />
}</p>
<p>new Thread(WriteToFile).Start();<br />
}</p>
<p>static void WriteToFile()<br />
{<br />
if (localMutex.WaitOne(100000, false))<br />
{<br />
StreamWriter sw = null;<br />
try<br />
{<br />
sw = new StreamWriter(path,true);<br />
sw.BaseStream.Seek(0, SeekOrigin.End);<br />
sw.WriteLine(&#8220;domain no.:&#8221; + AppDomain.CurrentDomain.Id + &#8220;, thread &#8221; + Thread.CurrentThread.ManagedThreadId + &#8221; :: &#8221; + System.DateTime.Now.ToString());<br />
Console.WriteLine(&#8220;domain no.:&#8221; + AppDomain.CurrentDomain.Id + &#8220;, thread &#8221; + Thread.CurrentThread.ManagedThreadId + &#8221; wrote. &#8221; );<br />
Thread.Sleep(200); // delay necesar observarii apelurilor in paralel<br />
sw.Flush();<br />
}<br />
catch (IOException e)<br />
{<br />
Console.WriteLine(e.Message);<br />
}<br />
finally<br />
{<br />
sw.Close();<br />
localMutex.ReleaseMutex();<br />
}<br />
}<br />
}<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/37/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/37/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=37&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/readerwriter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>Network Information Part 2.</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/network-information-part-2/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/network-information-part-2/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:13:03 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=36</guid>
		<description><![CDATA[using System; using System.Collections.Generic; using System.Text; using System.Net.NetworkInformation; using System.Xml; namespace NetworkInformation { class Program { static void Main(string[] args) { IPGlobalProperties props     = IPGlobalProperties.GetIPGlobalProperties(); NetworkInterface[] interfete = NetworkInterface.GetAllNetworkInterfaces(); //creez fisier XML XmlTextWriter textWriter = new XmlTextWriter(@&#8221;C:\Documents and Settings\admin\Desktop\SD\networkInfo.xml&#8221;, null); //deschid pentru a scrie textWriter.WriteStartDocument(); //scriu un comentariu in fisierul XML textWriter.WriteComment(&#8220;Interfete retea pentru &#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=36&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Net.NetworkInformation;<br />
using System.Xml;</p>
<p>namespace NetworkInformation<br />
{<br />
class Program<br />
{<br />
static void Main(string[] args)<br />
{<br />
IPGlobalProperties props     = IPGlobalProperties.GetIPGlobalProperties();<br />
NetworkInterface[] interfete = NetworkInterface.GetAllNetworkInterfaces();</p>
<p>//creez fisier XML<br />
XmlTextWriter textWriter = new XmlTextWriter(@&#8221;C:\Documents and Settings\admin\Desktop\SD\networkInfo.xml&#8221;, null);</p>
<p>//deschid pentru a scrie<br />
textWriter.WriteStartDocument();</p>
<p>//scriu un comentariu in fisierul XML<br />
textWriter.WriteComment(&#8220;Interfete retea pentru &#8221; + props.HostName);</p>
<p>//primul element<br />
textWriter.WriteStartElement(&#8220;host&#8221;);</p>
<p>foreach (NetworkInterface interfata in interfete)<br />
{<br />
IPInterfaceProperties ipprop = interfata.GetIPProperties();</p>
<p>//begin interface<br />
textWriter.WriteStartElement(&#8220;interface&#8221;);</p>
<p>//attribute<br />
textWriter.WriteAttributeString(&#8220;name&#8221;, interfata.Name);</p>
<p>textWriter.WriteStartElement(&#8220;description&#8221;);<br />
textWriter.WriteString(interfata.Description);<br />
textWriter.WriteEndElement();</p>
<p>textWriter.WriteStartElement(&#8220;type&#8221;);<br />
textWriter.WriteString(interfata.NetworkInterfaceType.ToString());<br />
textWriter.WriteEndElement();</p>
<p>if (interfata.NetworkInterfaceType == NetworkInterfaceType.Loopback)<br />
continue;</p>
<p>textWriter.WriteStartElement(&#8220;viteza&#8221;);<br />
textWriter.WriteString(interfata.Speed + &#8220;bps&#8221;);<br />
textWriter.WriteEndElement();</p>
<p>textWriter.WriteStartElement(&#8220;stare&#8221;);<br />
textWriter.WriteString(interfata.OperationalStatus.ToString());<br />
textWriter.WriteEndElement();</p>
<p>textWriter.WriteStartElement(&#8220;adresaFizica&#8221;);<br />
textWriter.WriteString(interfata.GetPhysicalAddress().ToString());<br />
textWriter.WriteEndElement();</p>
<p>textWriter.WriteStartElement(&#8220;sufixDNS&#8221;);<br />
textWriter.WriteString(ipprop.DnsSuffix);<br />
textWriter.WriteEndElement();</p>
<p>textWriter.WriteStartElement(&#8220;adreseIP&#8221;);<br />
UnicastIPAddressInformationCollection adreseIP = ipprop.UnicastAddresses;<br />
if (adreseIP.Count &gt; 0)<br />
{<br />
foreach (UnicastIPAddressInformation uni in adreseIP)<br />
{<br />
textWriter.WriteAttributeString(&#8220;adresaIPunicast&#8221;,uni.Address.ToString());</p>
<p>textWriter.WriteStartElement(&#8220;Mask&#8221;);<br />
textWriter.WriteString(uni.IPv4Mask.ToString());<br />
textWriter.WriteEndElement();</p>
<p>/*Console.WriteLine((DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime)).ToString());<br />
Console.Read();<br />
*/</p>
<p>/*<br />
textWriter.WriteStartElement(&#8220;DHCP lease time&#8221;);<br />
textWriter.WriteString((DateTime.UtcNow + TimeSpan.FromSeconds(uni.DhcpLeaseLifetime)).ToString());<br />
textWriter.WriteEndElement();*/<br />
}<br />
}<br />
textWriter.WriteEndElement();</p>
<p>//end interface<br />
textWriter.WriteEndElement();<br />
}<br />
textWriter.WriteEndElement();</p>
<p>// end the document.<br />
textWriter.WriteEndDocument();</p>
<p>// close writer<br />
textWriter.Close();</p>
<p>}<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/36/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/36/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/36/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/36/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/36/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=36&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/network-information-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>Ex3.</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/ex3/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/ex3/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:11:52 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=35</guid>
		<description><![CDATA[Careful Program.cs using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Threading; namespace Pr2 { class Program { public delegate void threadDelegate(string message); static public event threadDelegate unEvent; static void Main(string[] args) { bool dirValid = false; string path = &#8220;&#8221;; string responsePrompt=&#8221;"; while (!dirValid &#38;&#38; path != &#8220;q&#8221;) { Console.WriteLine(&#8220;\nIntroduceti calea spre director (sau [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=35&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Careful</span></p>
<p><strong>Program.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
using System.Threading;</p>
<p>namespace Pr2<br />
{<br />
class Program<br />
{</p>
<p>public delegate void threadDelegate(string message);</p>
<p>static public event threadDelegate unEvent;<br />
static void Main(string[] args)<br />
{<br />
bool dirValid = false;<br />
string path = &#8220;&#8221;;<br />
string responsePrompt=&#8221;";<br />
while (!dirValid &amp;&amp; path != &#8220;q&#8221;)<br />
{<br />
Console.WriteLine(&#8220;\nIntroduceti calea spre director (sau q pentru a iesi din bucla): &#8220;);<br />
path = Console.ReadLine();<br />
dirValid = dirExists(path);<br />
if (dirValid &amp;&amp; dirIsEmpty(path))<br />
{<br />
Console.WriteLine(&#8220;\nDirectorul a fost gasit dar este gol, generez fisiere.&#8221;);<br />
generator_de_fisiere(path);<br />
}<br />
}<br />
if (dirValid)<br />
Console.WriteLine(&#8220;\nDirectorul &#8221; + path + &#8221; a fost gasit.&#8221;);<br />
else<br />
{<br />
//s-a introdus q<br />
return;<br />
}<br />
responsePrompt = &#8220;&#8221;;<br />
while (responsePrompt != &#8220;n&#8221; &amp;&amp; responsePrompt != &#8220;N&#8221; &amp;&amp; responsePrompt != &#8220;y&#8221; &amp;&amp; responsePrompt != &#8220;Y&#8221;)<br />
{<br />
Console.WriteLine(&#8220;\nDoriti sa stergeti continutul fiserului &#8221; + path + &#8221; ? (y/n)&#8221;);<br />
responsePrompt = Console.ReadLine();<br />
}<br />
if (responsePrompt == &#8220;Y&#8221; || responsePrompt == &#8220;y&#8221;)<br />
{<br />
//stergere<br />
stergere(path);<br />
}<br />
Console.WriteLine(&#8220;\t\tApasati orice tasta pentru a termina executia programului.&#8221;);<br />
Console.ReadKey();<br />
}</p>
<p>static bool dirExists(string path)<br />
{<br />
return Directory.Exists(path);<br />
}</p>
<p>static bool dirIsEmpty(string path)<br />
{<br />
string[] fisiere = Directory.GetFiles(path);<br />
string[] directoare = Directory.GetDirectories(path);<br />
/*foreach (string s in fisiere)<br />
{<br />
Console.WriteLine(s);<br />
}<br />
foreach (string s in directoare)<br />
{<br />
Console.WriteLine(s);<br />
}*/<br />
if (fisiere.Length &gt; 0 || directoare.Length &gt; 0)<br />
return false;<br />
else<br />
return true;<br />
}</p>
<p>static void stergere(string path)<br />
{<br />
if (!dirExists(path))<br />
{<br />
Console.WriteLine(&#8220;Directorul nu exista.&#8221;);<br />
return;<br />
}<br />
if (dirIsEmpty(path))<br />
{<br />
Console.WriteLine(&#8220;Directorul &#8220;+path+&#8221; este gol.&#8221;);<br />
return;<br />
}<br />
//<br />
string[] fisiere = Directory.GetFiles(path);<br />
//stergere fisere<br />
Thread[] threads = new Thread[fisiere.Length];<br />
//event<br />
unEvent += new threadDelegate(afisare_mesaj);<br />
for (int i = 0; i &lt; fisiere.Length; i++)<br />
{<br />
threads[i] = new Thread(sterge_fisier);<br />
threads[i].Start(fisiere[i]);<br />
//Thread.Sleep(500);<br />
}<br />
//reapel pt stergere directoare<br />
string[] directoare = Directory.GetDirectories(path);<br />
foreach (string dir in directoare)<br />
{<br />
stergere(dir);<br />
Thread thDir = new Thread(stergere_director);<br />
thDir.Start(dir);<br />
}<br />
}</p>
<p>static void sterge_fisier(object path)<br />
{<br />
//stergerea logica<br />
stergere_logica(path as string);<br />
unEvent.Invoke(&#8220;a fost sters logic &#8220;+path);<br />
//stergerea fizica<br />
stergere_fizica(path as string);<br />
}</p>
<p>static void stergere_logica(string path)<br />
{<br />
byte[] bytes = File.ReadAllBytes(path);<br />
for (int i = 0; i &lt; bytes.Length; i++)<br />
bytes[i] = 0;<br />
File.WriteAllBytes(path, bytes);</p>
<p>}</p>
<p>static void stergere_fizica(string path)<br />
{<br />
File.Delete(path);<br />
}</p>
<p>static void afisare_mesaj(string message)<br />
{<br />
Console.WriteLine(&#8220;[{0}] &#8211; {1}.&#8221;,Thread.CurrentThread.ManagedThreadId,message);<br />
}</p>
<p>static void generator_de_fisiere(string path)<br />
{<br />
for (int i=0; i&lt;600; i++)<br />
{<br />
StringBuilder sb = new StringBuilder();<br />
sb.Append(path);<br />
sb.Append(&#8220;/&#8221;);<br />
sb.Append(i);<br />
sb.Append(&#8220;fis.txt&#8221;);<br />
//<br />
FileStream fs = File.Open(sb.ToString(), FileMode.OpenOrCreate, FileAccess.ReadWrite);</p>
<p>byte[] bytes = new byte[300];<br />
for (int j = 0; j &lt; 300; j++)<br />
bytes[j] = (byte) j;<br />
fs.Write(bytes, 0, 300);<br />
}<br />
}</p>
<p>static void stergere_director(object path)<br />
{<br />
while (Directory.GetFiles(path as string).Length != 0)<br />
{<br />
Thread.Sleep(100);<br />
}<br />
Directory.Delete(path as string);<br />
}</p>
<p>}<br />
}</p>
<p><strong>Description</strong></p>
<p>500 si 1000</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/35/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/35/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=35&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/ex3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>File comparation</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/file-comparation/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/file-comparation/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:10:16 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=34</guid>
		<description><![CDATA[Carefull Program.cs, Differ.cs Program.cs using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Threading; using System.Xml.Serialization; namespace p1exam { class Program { static string path1 = &#8220;f1.txt&#8221;; static string path2 = &#8220;f2.txt&#8221;; static int size = 0; static int random = 0; static int cat = 0; static int rest = 0; static List&#60;Differ&#62; colectie [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=34&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="color:#ff0000;">Carefull</span></p>
<p>Program.cs, Differ.cs</p>
<p><strong>Program.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.IO;<br />
using System.Threading;<br />
using System.Xml.Serialization;</p>
<p>namespace p1exam<br />
{<br />
class Program<br />
{<br />
static string path1 = &#8220;f1.txt&#8221;;<br />
static string path2 = &#8220;f2.txt&#8221;;<br />
static int size = 0;<br />
static int random = 0;<br />
static int cat = 0;<br />
static int rest = 0;<br />
static List&lt;Differ&gt; colectie = new List&lt;Differ&gt;();<br />
static Differ diferit = null;<br />
static   FileStream f1 ;<br />
static    FileStream f2;<br />
static object obj = new object();</p>
<p>internal   class douaValori<br />
{<br />
public  int poz0;<br />
public  int poz1;</p>
<p>}<br />
public  static void comparare(object interv)<br />
{<br />
douaValori poz = (douaValori)interv;</p>
<p>int[] p=new int[2];<br />
p[0]=poz.poz0;<br />
p[1]=poz.poz1;</p>
<p>lock (obj)<br />
{<br />
Console.WriteLine(&#8220;Pozitii in apel:&#8221; +p[0] + &#8221; to &#8221; + p[1]);</p>
<p>f1.Seek((long)p[0], SeekOrigin.Begin);<br />
f2.Seek((long)p[0], SeekOrigin.Begin);</p>
<p>byte[] buff1 = new byte[p[1] &#8211; p[0] + 1];<br />
byte[] buff2 = new byte[p[1] &#8211; p[0] + 1];</p>
<p>f1.Read(buff1, 0, p[1] &#8211; p[0]);<br />
f2.Read(buff2, 0, p[1] &#8211; p[0]);</p>
<p>for (int i = 0; i &lt; buff1.Length; i++)<br />
{<br />
if (buff1[i] != buff2[i])<br />
{<br />
diferit = new Differ();<br />
diferit.Pozitie = i;<br />
diferit.Bit_diferit = buff1[i];</p>
<p>lock (colectie)<br />
{<br />
colectie.Add(diferit);<br />
}<br />
}<br />
}</p>
<p>}</p>
<p>}</p>
<p>static void Main(string[] args)<br />
{</p>
<p>f1 = new FileStream(path1, FileMode.Open, FileAccess.Read);<br />
f2 = new FileStream(path2, FileMode.Open, FileAccess.Read);</p>
<p>if (f1.Length == f2.Length)<br />
{<br />
Console.WriteLine(&#8220;Sunt egale ca dimensiune!!!&#8221;);<br />
size = (int)f1.Length;<br />
Random r = new Random();<br />
random = r.Next(0, size);<br />
if (size % random != 0)<br />
{<br />
int[] poz = new int[2];<br />
int i=0;<br />
cat = (int)(size / random);<br />
rest = size &#8211; (random * cat);<br />
Thread[] threads = new Thread[cat + 1];<br />
douaValori pozz = null;<br />
poz[0] = 0;<br />
for (i = 0; i &lt; cat; i++)<br />
{<br />
if (i != 0)<br />
poz[0] = poz[0] + random + 1;<br />
poz[1] = poz[1] + random;<br />
pozz = new douaValori();<br />
pozz.poz0 = poz[0];<br />
pozz.poz1 = poz[1];<br />
Console.WriteLine(&#8220;Pozitii inainte de apel:&#8221; + poz[0] + &#8221; to &#8221; + poz[1]);<br />
threads[i] = new Thread(new ParameterizedThreadStart(comparare));<br />
threads[i].Start(pozz);<br />
}</p>
<p>//restul<br />
poz[0] = poz[1] + 1;<br />
poz[1] = poz[1] + rest;<br />
pozz = new douaValori();<br />
pozz.poz0 = poz[0];<br />
pozz.poz1 = poz[1];<br />
threads[i] = new Thread(new ParameterizedThreadStart(comparare));<br />
threads[i].Start(pozz);</p>
<p>for (int j = 0; j &lt; (cat+1); j++)<br />
threads[j].Join();</p>
<p>XmlSerializer x = new XmlSerializer(typeof(List&lt;Differ&gt;));<br />
TextWriter w = new StreamWriter(&#8220;list.xml&#8221;);<br />
x.Serialize(w, colectie);<br />
w.Close();</p>
<p>}</p>
<p>}<br />
else<br />
Console.WriteLine(&#8220;Nu sunt egale ca dimensiune!!!&#8221;);<br />
f1.Close();<br />
f2.Close();<br />
Console.ReadKey();<br />
}<br />
}<br />
}</p>
<p><strong>Differ.cs</strong></p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;</p>
<p>namespace p1exam<br />
{<br />
public class Differ<br />
{<br />
private int pozitie;</p>
<p>public int Pozitie<br />
{<br />
get { return pozitie; }<br />
set { pozitie = value; }<br />
}<br />
private byte bit_diferit;</p>
<p>public byte Bit_diferit<br />
{<br />
get { return bit_diferit; }<br />
set { bit_diferit = value; }<br />
}</p>
<p>public Differ()<br />
{ }<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/34/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/34/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=34&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/file-comparation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>Web application example</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/web-application-example/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/web-application-example/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:08:46 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Samples]]></category>
		<category><![CDATA[web application]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=33</guid>
		<description><![CDATA[Default.aspx &#60;%@ Page Language=&#8221;C#&#8221; AutoEventWireup=&#8221;true&#8221;  CodeFile=&#8221;Default.aspx.cs&#8221; Inherits=&#8221;_Default&#8221; %&#62; &#60;!DOCTYPE html PUBLIC &#8220;-//W3C//DTD XHTML 1.0 Transitional//EN&#8221; &#8220;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&#62; &#60;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221; &#62; &#60;head runat=&#8221;server&#8221;&#62; &#60;title&#62;Procesare String&#60;/title&#62; &#60;/head&#62; &#60;body&#62; &#60;form id=&#8221;form1&#8243; runat=&#8221;server&#8221;&#62; &#60;div style=&#8221;border:solid 1px #ccc;padding:10px;width:500px;height:500px;position:relative; background-color:#eee&#8221;&#62; &#60;!&#8211; String Label &#8211;&#62; &#60;asp:Label ID=&#8221;labelString&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:102;font-family:Arial; font-size:small;&#8221; Text=&#8221;String:&#8221;&#62;&#60;/asp:Label&#62; &#60;br /&#62; &#60;!&#8211; Text Input &#8211;&#62; &#60;asp:TextBox ID=&#8221;textBox&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:105;font-family:Arial; font-size:small;&#8221; Width=&#8221;256px&#8221;&#62;&#60;/asp:TextBox&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=33&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Default.aspx</strong></p>
<p>&lt;%@ Page Language=&#8221;C#&#8221; AutoEventWireup=&#8221;true&#8221;  CodeFile=&#8221;Default.aspx.cs&#8221; Inherits=&#8221;_Default&#8221; %&gt;</p>
<p>&lt;!DOCTYPE html PUBLIC &#8220;-//W3C//DTD XHTML 1.0 Transitional//EN&#8221; &#8220;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#8221;&gt;</p>
<p>&lt;html xmlns=&#8221;http://www.w3.org/1999/xhtml&#8221; &gt;<br />
&lt;head runat=&#8221;server&#8221;&gt;<br />
&lt;title&gt;Procesare String&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;form id=&#8221;form1&#8243; runat=&#8221;server&#8221;&gt;<br />
&lt;div style=&#8221;border:solid 1px #ccc;padding:10px;width:500px;height:500px;position:relative; background-color:#eee&#8221;&gt;<br />
&lt;!&#8211; String Label &#8211;&gt;<br />
&lt;asp:Label ID=&#8221;labelString&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:102;font-family:Arial; font-size:small;&#8221; Text=&#8221;String:&#8221;&gt;&lt;/asp:Label&gt;<br />
&lt;br /&gt;</p>
<p>&lt;!&#8211; Text Input &#8211;&gt;<br />
&lt;asp:TextBox ID=&#8221;textBox&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:105;font-family:Arial; font-size:small;&#8221; Width=&#8221;256px&#8221;&gt;&lt;/asp:TextBox&gt;</p>
<p>&lt;!&#8211; Validare Text Input&#8211;&gt;<br />
&lt;asp:CustomValidator ID=&#8221;ValidateText&#8221; runat=&#8221;server&#8221; ControlToValidate=&#8221;textBox&#8221; ErrorMessage=&#8221;Nu este text!&#8221; OnServerValidate=&#8221;ValidateText_ServerValidate&#8221; Style=&#8221;z-index:108;&#8221;&gt;<br />
&lt;/asp:CustomValidator&gt;<br />
&lt;br /&gt;&lt;br /&gt;</p>
<p>&lt;!&#8211; Tip Procesare &#8211;&gt;<br />
&lt;asp:Label ID=&#8221;labelTipProcesare&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:104;font-family:Arial; font-size:small;&#8221; Text=&#8221;Tip Procesare&#8221;&gt;&lt;/asp:Label&gt;<br />
&lt;br /&gt;<br />
&lt;!&#8211; DropDown List &#8211;&gt;<br />
&lt;asp:DropDownList ID=&#8221;dropDownList&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index: 109;font-family:Arial; font-size:small;&#8221; Width=&#8221;256px&#8221;&gt;<br />
&lt;asp:ListItem Value=&#8221;0&#8243;&gt;Litere mici&lt;/asp:ListItem&gt;<br />
&lt;asp:ListItem Value=&#8221;1&#8243;&gt;Litere mari&lt;/asp:ListItem&gt;<br />
&lt;asp:ListItem Value=&#8221;2&#8243;&gt;Valoare numerica&lt;/asp:ListItem&gt;<br />
&lt;/asp:DropDownList&gt;</p>
<p>&lt;!&#8211; Nr. Prim &#8211;&gt;<br />
&lt;asp:Label ID=&#8221;labelNrPrim&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index: 103;font-family:Arial; font-size:small;&#8221; Text=&#8221;Nr.Prim:&#8221;&gt;&lt;/asp:Label&gt;<br />
&lt;!&#8211; Nr. Prim Input &#8211;&gt;<br />
&lt;asp:TextBox ID=&#8221;nrBox&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:106;font-family:Arial; font-size:small;&#8221; Width=&#8221;118px&#8221;&gt;&lt;/asp:TextBox&gt;</p>
<p>&lt;!&#8211; Validare Nr. Prim &#8211;&gt;<br />
&lt;asp:CustomValidator ID=&#8221;ValidateNrPrim&#8221; runat=&#8221;server&#8221; ControlToValidate=&#8221;nrBox&#8221; ErrorMessage=&#8221;Nu este nr. prim!&#8221; OnServerValidate=&#8221;ValidateNrPrim_ServerValidate&#8221; Style=&#8221;z-index:107;&#8221;&gt;<br />
&lt;/asp:CustomValidator&gt;&lt;br /&gt;&lt;br /&gt;</p>
<p>&lt;!&#8211; Proceseaza &#8211;&gt;<br />
&lt;asp:Button ID=&#8221;butonProceseaza&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:100;font-family:Arial; font-size:small;&#8221; Text=&#8221;Proceseaza&#8221; Width=&#8221;107px&#8221; OnClick=&#8221;Proceseaza_Click&#8221; /&gt;<br />
&lt;br /&gt;&lt;br /&gt;<br />
&lt;!&#8211; ListBox cu Select &#8211;&gt;<br />
&lt;asp:ListBox ID=&#8221;listBox&#8221; runat=&#8221;server&#8221; Height=&#8221;135px&#8221; SelectionMode=&#8221;Multiple&#8221; Style=&#8221;z-index:110;&#8221; Width=&#8221;300px&#8221;&gt;<br />
&lt;/asp:ListBox&gt;<br />
&lt;br /&gt;&lt;br /&gt;<br />
&lt;!&#8211; Sterge &#8211;&gt;<br />
&lt;asp:Button ID=&#8221;buttonSterge&#8221; runat=&#8221;server&#8221; Style=&#8221;z-index:111;font-family:Arial; font-size:small;&#8221; Text=&#8221;Sterge&#8221; Width=&#8221;107px&#8221; OnClick=&#8221;Sterge_Click&#8221; /&gt;<br />
&lt;/div&gt;<br />
&lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
<p><strong>Default.aspx.cs</strong></p>
<p>using System;<br />
using System.Data;<br />
using System.Configuration;<br />
using System.Web;<br />
using System.Web.Security;<br />
using System.Web.UI;<br />
using System.Web.UI.WebControls;<br />
using System.Web.UI.WebControls.WebParts;<br />
using System.Web.UI.HtmlControls;<br />
using System.Web.UI.MobileControls;<br />
using System.Collections.Generic;</p>
<p>public partial class _Default : System.Web.UI.Page<br />
{<br />
protected void Page_Load(object sender, EventArgs e)<br />
{</p>
<p>}<br />
protected void ValidateNrPrim_ServerValidate(object source, ServerValidateEventArgs args)<br />
{<br />
int nr = Int32.Parse(nrBox.Text);<br />
args.IsValid = true;<br />
for(int i=2;i&lt;=nr/2;i++)<br />
if (nr % i == 0)<br />
{<br />
args.IsValid = false;<br />
break;<br />
}<br />
}</p>
<p>protected void ValidateText_ServerValidate(object source, ServerValidateEventArgs args)<br />
{<br />
/*double nr;<br />
string text = textBox.Text.Trim();<br />
bool b = double.TryParse(text, out nr);<br />
if (b)<br />
args.IsValid = false;<br />
else<br />
args.IsValid = true;<br />
*/<br />
string text  = textBox.Text.Trim();<br />
args.IsValid = true;<br />
for (int i = 0; i &lt; text.Length; i++)<br />
if (char.IsDigit(text[i]) == true)<br />
args.IsValid = false;<br />
}</p>
<p>protected void Proceseaza_Click(object sender, EventArgs e)<br />
{<br />
switch (dropDownList.SelectedValue)<br />
{<br />
case &#8220;0&#8243;:<br />
listBox.Items.Add(textBox.Text.ToLower());<br />
break;<br />
case &#8220;1&#8243;:<br />
listBox.Items.Add(textBox.Text.ToUpper());<br />
break;<br />
case &#8220;2&#8243;:<br />
int prim = Int32.Parse(nrBox.Text);<br />
string s = textBox.Text;<br />
int rez=0;<br />
for (int i=0;i&lt;s.Length &#8211; 1;i++)<br />
rez = prim *((int)s[i])+(int)s[i+1];<br />
listBox.Items.Add(rez.ToString());<br />
break;<br />
}<br />
}<br />
protected void Sterge_Click(object sender, EventArgs e)<br />
{<br />
ListItemCollection collection = new ListItemCollection();<br />
foreach (ListItem item in listBox.Items)<br />
{<br />
if (item.Selected)<br />
collection.Add(item);<br />
}<br />
foreach (ListItem item in collection)<br />
{<br />
listBox.Items.Remove(item);<br />
}<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/33/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/33/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/33/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/33/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/33/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=33&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/web-application-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
		<item>
		<title>Multiple Threads</title>
		<link>http://csharphelp.wordpress.com/2008/06/25/multiple-threads/</link>
		<comments>http://csharphelp.wordpress.com/2008/06/25/multiple-threads/#comments</comments>
		<pubDate>Wed, 25 Jun 2008 23:04:03 +0000</pubDate>
		<dc:creator>Garbage Collector</dc:creator>
				<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://csharphelp.wordpress.com/?p=32</guid>
		<description><![CDATA[Program.cs using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.IO; namespace l3p1 { class Program { public const int NUM_THREADS = 10; static FileStream myFile = new FileStream(@&#8221;E:\1311\lab3\l3p1\l3p1\f.txt&#8221;, FileMode.Open, FileAccess.Read); static StreamReader fileRead = new StreamReader(myFile); static void Main(string[] args) { List&#60;Thread&#62; readThreads = new List&#60;Thread&#62;(); for (int i = 0; i &#60; NUM_THREADS; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=32&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Program.cs</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using System.IO;</p>
<p>namespace l3p1<br />
{<br />
class Program<br />
{<br />
public const int NUM_THREADS = 10;<br />
static FileStream myFile = new FileStream(@&#8221;E:\1311\lab3\l3p1\l3p1\f.txt&#8221;, FileMode.Open,<br />
FileAccess.Read);<br />
static StreamReader fileRead = new StreamReader(myFile);<br />
static void Main(string[] args)<br />
{<br />
List&lt;Thread&gt; readThreads = new List&lt;Thread&gt;();<br />
for (int i = 0; i &lt; NUM_THREADS; i++)<br />
{<br />
Reader rd = new Reader(myFile);<br />
Thread myThread = new Thread(rd.DoWork);<br />
myThread.Name = i.ToString();<br />
readThreads.Add(myThread);<br />
}<br />
foreach (Thread t in readThreads)<br />
{<br />
t.Start();<br />
}<br />
Console.ReadLine();<br />
}</p>
<p>static void readFile()<br />
{<br />
while (!fileRead.EndOfStream)<br />
{<br />
string line = fileRead.ReadLine();<br />
System.Console.WriteLine(&#8220;Thread-ul {0} a citit:{1}&#8221;, Thread.CurrentThread.Name, line);<br />
Thread.Sleep(30);<br />
}<br />
}<br />
}<br />
}</p>
<p>Reader.cs</p>
<p>using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Threading;<br />
using System.IO;</p>
<p>namespace l3p1<br />
{<br />
class Reader<br />
{<br />
FileStream _file;</p>
<p>public Reader(FileStream file)<br />
{<br />
_file = file;<br />
}</p>
<p>public void DoWork()<br />
{<br />
while (true)<br />
{<br />
StreamReader myStrRead = new StreamReader(_file);<br />
while (myStrRead.EndOfStream)<br />
{<br />
Thread.Sleep(20);<br />
}<br />
string line = myStrRead.ReadLine();<br />
_file.Seek(0, SeekOrigin.Begin);<br />
Console.WriteLine(&#8220;{0}(r):{1}&#8221;, Thread.CurrentThread.Name, line);<br />
Thread.Sleep(30);<br />
}<br />
}<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/csharphelp.wordpress.com/32/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/csharphelp.wordpress.com/32/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/csharphelp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/csharphelp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/csharphelp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/csharphelp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/csharphelp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/csharphelp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/csharphelp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/csharphelp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/csharphelp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/csharphelp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/csharphelp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/csharphelp.wordpress.com/32/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/csharphelp.wordpress.com/32/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/csharphelp.wordpress.com/32/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=csharphelp.wordpress.com&amp;blog=4042473&amp;post=32&amp;subd=csharphelp&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://csharphelp.wordpress.com/2008/06/25/multiple-threads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/e4bb9c8537e3481f8ab76fa4d172d54b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">Garbage Collector</media:title>
		</media:content>
	</item>
	</channel>
</rss>
