sexta-feira, 7 de outubro de 2011

Simple Parallel ForEach in C#.NET 2.0

A simple Parallel ForEach implementation for Microsoft C#.NET 2.0:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace ParallelTest
{
    public class Parallel
    {
        public delegate void Process();

        public static void ForEach<T>(IEnumerable<T> enumerable, Action<T> action) 
        {
            IEnumerator<T> enumerator = enumerable.GetEnumerator();

            int threadCount = Environment.ProcessorCount;

            Process process = delegate()
            {
                while (true)
                {
                    T item = default(T);

                    lock (enumerator)
                    {
                        if (enumerator.MoveNext())
                            item = enumerator.Current;
                        else
                            return;
                    }

                    action(item);
                }
            };

            IAsyncResult[] asyncResults = new IAsyncResult[threadCount];

            for (int i = 0; i < threadCount; ++i)
                asyncResults[i] = process.BeginInvoke(null, null);

            for (int i = 0; i < threadCount; ++i)
                process.EndInvoke(asyncResults[i]);
        }
    }
}

segunda-feira, 3 de outubro de 2011

How to post code?

Follow the example below:


<pre class="brush: html"><h1>
html code here...</h1>
</pre>


Should render like

html code here...

List of available languages:


  • cpp
  • csharp
  • css
  • html
  • java
  • jscript
  • php
  • python
  • ruby
  • sql
  • vb
  • xml
  • perl

syntax highlighting tutorial

Syntax coloring test

public class Syntax
{
    abstract Language Language { get; set; }
}

Source code:

<pre class="brush: csharp">
public class Syntax
{
    abstract Language Language { get; set; }
}
</pre>