<?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/"
	>

<channel>
	<title>C# Help &#187; Articles</title>
	<atom:link href="http://www.csharphelp.com/category/articles/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharphelp.com</link>
	<description>C# Tutorial and Guides</description>
	<lastBuildDate>Tue, 07 Feb 2012 01:03:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Compressing in Memory using MemoryStream</title>
		<link>http://www.csharphelp.com/2011/01/compressing-in-memory-using-memorystream/</link>
		<comments>http://www.csharphelp.com/2011/01/compressing-in-memory-using-memorystream/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 05:34:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[C# Language]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[MemoryStream]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2497</guid>
		<description><![CDATA[Sometimes you need to compress entirely in memory. Here’s how to use a MemoryStream for this purpose: byte[] data = new byte[1000]; // We would expect a good compression ratio from an empty array! var msObj = new MemoryStream(); using (Stream ds = new DeflateStream (msObj, CompressionMode.Compress)) ds.Write (data, 0, data.Length); byte[] compressed = msObj.ToArray(); Console.WriteLine (compressed.Length); [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need to compress entirely in memory. Here’s how to use a MemoryStream for this purpose:</p>
<pre>
byte[] data = new byte[1000]; // We would expect a good compression ratio from an empty array!
var msObj = new MemoryStream();
using (Stream ds = new DeflateStream (msObj, CompressionMode.Compress))
ds.Write (data, 0, data.Length);
byte[] compressed = msObj.ToArray();
Console.WriteLine (compressed.Length); // 113
// Decompress back to the data array:
msObj= new MemoryStream (compressed);
using (Stream ds = new DeflateStream (msObj, CompressionMode.Decompress))
for (int i = 0; i &lt; 1000; i += ds.Read (data, i, 1000 - i));
</pre>
<p>In the above code the using statement wrapped around the DeflateStream closes it in  textbook fashion, and flushes  unwritten buffers in the process. The structure also closes the MemoryStream it wraps and so we will  then need to call ToArray to extract its data.<br />
The below code shows an alternative which avoids closing the MemoryStream:</p>
<pre>
byte[] data = new byte[1000];
MemoryStream  msObj= new MemoryStream();
using (Stream ds = new DeflateStream (ms, CompressionMode.Compress, true))
ds.Write (data, 0, data.Length);
Console.WriteLine (msObj.Length); // 113
msObj.Position = 0;
using (Stream ds = new DeflateStream (msObj, CompressionMode.Decompress))
for (int i = 0; i &lt; 1000; i += ds.Read (data, i, 1000 - i));
</pre>
<p>Here, the additional flag which is sent to the DeflateStream’s constructor instructs it not to follow the usual protocol of taking the underlying stream with it in disposal. Thus, the MemoryStream is still left open, allowing us to reposition it  to zero and then reread it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/01/compressing-in-memory-using-memorystream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compression Using DeflateStream and GZipStream</title>
		<link>http://www.csharphelp.com/2011/01/compression-deflatestream-gzipstream/</link>
		<comments>http://www.csharphelp.com/2011/01/compression-deflatestream-gzipstream/#comments</comments>
		<pubDate>Tue, 11 Jan 2011 05:31:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[C# Language]]></category>
		<category><![CDATA[compression]]></category>
		<category><![CDATA[DeflateStream]]></category>
		<category><![CDATA[GZipStream]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2491</guid>
		<description><![CDATA[The .NET System.IO.Compression namespace provides two general purpose compressions streams &#8211;  DeflateStream and GZipStream. Both these compressions streams use popular compression algorithm which is similar to that used by the ZIP format. The difference is that GZipStream writes an additional protocol at the start and at the end which includes a CRC to detect  errors. The GZipStream also [...]]]></description>
			<content:encoded><![CDATA[<p>The .NET System.IO.Compression namespace provides two general purpose compressions streams &#8211;  <strong>DeflateStream </strong>and <strong>GZipStream</strong>.</p>
<p>Both these compressions streams use popular compression algorithm which is similar to that used by the ZIP format. The difference is that GZipStream writes an additional protocol at the start and at the end which includes a CRC to detect  errors. The GZipStream also conforms to a standard recognized by other software.</p>
<p>Both DeflateStream and  GZipStream allow reading and writing, with the below provisos:</p>
<ul>
<li>Always write to the stream when compressing.</li>
<li>Always read from the stream when decompressing.</li>
</ul>
<p>DeflateStream and GZipStream are both decorators &#8211;  compress or decompress data from another stream which is supplied in the construction. In the below sample, the code compresses and decompresses a series of bytes, using a FileStream as the backing store:</p>
<pre>
using (Stream s = File.Create ("compressed.bin"))
using (Stream ds = new DeflateStream (s, CompressionMode.Compress))
for (byte i = 0; i &lt; 100; i++)
ds.WriteByte (i);
using (Stream s = File.OpenRead ("compressed.bin"))
using (Stream ds = new DeflateStream (s, CompressionMode.Decompress))
for (byte i = 0; i &lt; 100; i++)
Console.WriteLine (ds.ReadByte()); // Writes 0 to 99</pre>
<p>Note that even with the smaller of the two algos, the compressed file is 241 bytes which is more than twice the size of the original file. Thus, compression does not work well with dense  nonrepetitive binary files. Compression is instead a better fit for files such as text text files as shown in the example below:</p>
<pre>
string[] words = "The quick brown fox jumps over the lazy dog".Split();
Random rand = new Random();
using (Stream s = File.Create ("compressed.bin"))
using (Stream ds = new DeflateStream (s, CompressionMode.Compress))

using (TextWriter w = new StreamWriter (ds))

for (int i = 0; i &lt; 1000; i++)
w.Write (words [rand.Next (words.Length)] + " ");
Console.WriteLine (new FileInfo ("compressed.bin").Length); // 1073
using (Stream s = File.OpenRead ("compressed.bin"))
using (Stream ds = new DeflateStream (s, CompressionMode.Decompress))
using (TextReader r = new StreamReader (ds))
Console.Write (r.ReadToEnd()); // Output below:
lazy lazy the fox the quick The brown fox jumps over fox over fox The
brown brown brown over brown quick fox brown dog dog lazy fox dog brown
over fox jumps lazy lazy quick The jumps fox jumps The over jumps dog..</pre>
<p>In the above sample, DeflateStream efficiently compresses to 1,073 bytes which is just slightly over one byte per word.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/01/compression-deflatestream-gzipstream/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the Parallel Class in C# &#8211; Run Tasks in Parallel on Multiple Processors or Cores</title>
		<link>http://www.csharphelp.com/2010/07/using-the-parallel-class-in-c-run-tasks-in-parallel-on-multiple-processors-or-cores/</link>
		<comments>http://www.csharphelp.com/2010/07/using-the-parallel-class-in-c-run-tasks-in-parallel-on-multiple-processors-or-cores/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 12:04:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Parallel]]></category>
		<category><![CDATA[Relection]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2362</guid>
		<description><![CDATA[The Parallel Class can be used to split up the tasks which work on the data. For example if you have iterative code that looks like the below code: //6 parts of the book string[] inputTextFiles = { “part1.txt”, “part2.txt”, “part3.txt”, “part4.txt”, “part5.txt”, “part6.txt” }; foreach (string file in inputTextFiles) { string contentStr = File.ReadAllText(file); CountCharacters(contentStr); [...]]]></description>
			<content:encoded><![CDATA[<p>The Parallel Class can be used to split up the tasks which work on the data. For example if you have iterative code that looks like the below code:</p>
<pre>//6 parts of the book
string[] inputTextFiles =
{
“part1.txt”, “part2.txt”, “part3.txt”,
“part4.txt”, “part5.txt”, “part6.txt”
};

foreach (string file in inputTextFiles)

{
string contentStr = File.ReadAllText(file);
CountCharacters(contentStr);
CountWords(contentStr);
}</pre>
<p>The Parallel class can be used with lambda expressions to parallelize the two calculation methods:</p>
<pre>foreach(string file in inputFiles)

{
string contentStr = File.ReadAllText(file);
Parallel.Invoke(
() =&gt; CountCharacters(contentStr),
() =&gt; CountWords(contentStr)
);
}</pre>
<p>The results from using the Parallel Class to split tasks are normally not as dramatic  as using <a href="http://www.csharphelp.com/2010/07/using-the-parallel-class-in-c/">Parallel to split data across processors</a> but they are still significant.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2010/07/using-the-parallel-class-in-c-run-tasks-in-parallel-on-multiple-processors-or-cores/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the Parallel Class in C# &#8211; Processing Data in Parallel across Multiple Processors or Cores</title>
		<link>http://www.csharphelp.com/2010/07/using-the-parallel-class-in-c/</link>
		<comments>http://www.csharphelp.com/2010/07/using-the-parallel-class-in-c/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 11:46:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Parallel]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2357</guid>
		<description><![CDATA[The new Task Parallel Library introduced with .NET 4, allows you to easily split code execution onto multiple processors without using threads or locks. For a task which is easily split into independent sub-tasks, or data that can be partitioned and computed separately you can use the Parallel class in System.Threading to assign tasks to [...]]]></description>
			<content:encoded><![CDATA[<p>The new Task Parallel Library introduced with .NET 4, allows you to easily split  code execution onto multiple processors without using threads or locks.</p>
<p>For a task which is easily split into independent sub-tasks, or data that can be partitioned and computed separately you can use the Parallel class in  System.Threading  to assign tasks to be automatically scheduled and then wait for the tasks to be  completed. The Parallel class will automatically scale to the number of available processors or cores.</p>
<h3>Process Data in Parallel across Multiple Processors or Cores</h3>
<p>When you have a  data set which  can be split over multiple processors and then processed independently, you should use constructs such as Parallel.For(), in the below example this is demonstrated for  computing prime numbers:</p>
<pre>using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace  PrimesDemo
{
class Program
{
static void Main(string[] args)
{
int maxPrimes = 1000000;
int maxNumber = 200000000;
long primesFound = 0;
Console.WriteLine(“Iterative”);
Stopwatch watchObj = new Stopwatch();
watchObj.Start();

for (UInt32 i = 0; i &lt; maxNumber; ++i) { if (IsPrime(i)) { Interlocked.Increment(ref primesFound); if (primesFound &gt; maxPrimes)
{
Console.WriteLine(“Last prime found was: {0:N0}”,
i);
break;
}
}
}
watchObj.Stop();
Console.WriteLine(“Found {0:N0} prime numbers in {1}”,
primesFound, watchObj.Elapsed);
watchObj.Reset();
primesFound = 0;
Console.WriteLine(“Parallel”);
watchObj.Start();
// to stop the loop, there is an overload that takes Action
Parallel.For(0, maxNumber, (i, loopState) =&gt;
{
if (IsPrime((UInt32)i))
{
Interlocked.Increment(ref primesFound);
if (primesFound &gt; maxPrimes)
{
Console.WriteLine(“Last prime found was: {0:N0}”,
i);
loopState.Stop();
}
}
});
watchObj.Stop();
Console.WriteLine(“Found {0:N0} prime numbers in {1}”,
primesFound, watchObj.Elapsed);
Console.ReadKey();
}
public static bool IsPrime(UInt32 number)
{

//check for evenness
if (number % 2 == 0)
{
if (number == 2)
return true;
return false;
}

UInt32 max = (UInt32)Math.Sqrt(number);
for (UInt32 i = 3; i &lt;= max; i += 2)
{
if ((number % i) == 0)
{
return false;
}
}
return true;
}
}
}</pre>
<p>When you run this note the drastically reduced time using Parallel, also note  that the  results are not  necessarily in order and that the parallel evaluation of the data does not arrive at the same results as the iterative evaluation. As opposed to running sequentially from 1 to 200,000,000 until one million primes are found, the input space is instead divided up and therefore it is possible to get different results in some circumstances.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2010/07/using-the-parallel-class-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Azure CDN (Content Delivery Network) In Your App</title>
		<link>http://www.azuresupport.com/2010/02/azure-cdn-content-delivery-network-app/</link>
		<comments>http://www.azuresupport.com/2010/02/azure-cdn-content-delivery-network-app/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 10:50:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[Windows Azure]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2018</guid>
		<description><![CDATA[The Windows Azure CDN is a convenient way for application developers to minimize latency that geographically dispersed application users will experience. As of as of February 2010, the Azure CDN is currently in CTP and incurring no charges.]]></description>
			<content:encoded><![CDATA[<p>The Windows Azure CDN is a convenient way for application developers to minimize latency that geographically dispersed application users will experience. As of  as of February 2010, the Azure CDN is currently in CTP and incurring no charges.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.azuresupport.com/2010/02/azure-cdn-content-delivery-network-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Azure Storage to an Azure Application</title>
		<link>http://www.azuresupport.com/2010/02/add-azure-storage-to-an-azure-application/</link>
		<comments>http://www.azuresupport.com/2010/02/add-azure-storage-to-an-azure-application/#comments</comments>
		<pubDate>Tue, 02 Feb 2010 10:49:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[Windows Azure]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2015</guid>
		<description><![CDATA[We previously looked at Creating an Azure App in Visual Studio and then Deploying the App on Azure . The application we built was a very simple one requiring no storage, we now turn our attention to extending the demo application to include Azure Storage. For this example we will use the blob storage in [...]]]></description>
			<content:encoded><![CDATA[<p>We previously looked at Creating an Azure App in Visual Studio and then Deploying the App on Azure . The application we built was a very simple one requiring no storage, we now turn our attention to extending the demo application to include Azure Storage. For this example we will use the blob storage in a simple Web Role.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.azuresupport.com/2010/02/add-azure-storage-to-an-azure-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

