Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Static Code Analysis

, 15 Mar 2010
A static code analyzer building method call networks + sample applications.
CodeAnalyzerAndToDoSample.zip
Generator
bin
.noclean
Arebis.CodeAnalysis.Static.dll
Arebis.CodeGeneration.dll
Arebis.CodeGeneration.VisualStudio.dll
Arebis.CodeGenerator.dll
Arebis.Common.dll
CGen.exe
Content.cst
Content.cst.gen
COperation.cst
COperation.cst.gen
files
GeneratorMain.cst
GeneratorMain.cst.gen
Index.cst
Index.cst.gen
IndexTop.cst
IndexTop.cst.gen
SOperation.cst
SOperation.cst.gen
UIOperation.cst
UIOperation.cst.gen
Output
SampleApp
LocalTestRun.testrunconfig
ToDoApplication
Infrastructure
Properties
Service References
ToDoContract
Properties
ToDoSample.vsmdi
ToDoService
Entities.edmx
Infrastructure
Properties
ToDoSample.Service.csproj.user
ToDoSampleDb.mdf
ToDoSampleDb_log.ldf
ToDoUnitTests
Properties
Testing
ToDoSample.UnitTests.csproj.user
UnitTests
StaticCodeAnalyzer
Arebis.CodeAnalysis.Static
Arebis.CodeAnalysis.Static.csproj.user
CodeModel.cd
Processors
Rules
Properties
Arebis.Common
Arebis
Collections
Generic
Reflection
Properties
System
Collections
Generic
Net2
Threading
Net2
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace System.Threading.Net2
{
	public delegate void ForDelegate(int i);

	public delegate void ThreadDelegate();

    [Obsolete("Prefer using the System.Threading.Parallel class from System.Threading.dll in the Microsoft Parallel Extensions.", false)]
	public static class Parallel
	{
		/// <summary>
		/// Parallel ForEach method.
		/// </summary>
		/// <remarks>Source: http://www.codeproject.com/KB/dotnet/PoorMansParallelForEach.aspx</remarks>
		public static void ForEach<T>(IEnumerable<T> enumerable, Action<T> action)
		{
			int threadCount = Environment.ProcessorCount;

			object syncRoot = new object();

			if (enumerable == null) return;

			IEnumerator<T> enumerator = enumerable.GetEnumerator();

			InvokeAsync<T> del = InvokeAction;

			T[] seedItemArray = new T[threadCount];
			List<IAsyncResult> resultList = new List<IAsyncResult>(threadCount);

			for (int i = 0; i < threadCount; i++)
			{
				bool moveNext;

				lock (syncRoot)
				{
					moveNext = enumerator.MoveNext();
					seedItemArray[i] = enumerator.Current;
				}

				if (moveNext)
				{
					IAsyncResult iAsyncResult = del.BeginInvoke
			 (enumerator, action, seedItemArray[i], syncRoot, i, null, null);
					resultList.Add(iAsyncResult);
				}
			}

			foreach (IAsyncResult iAsyncResult in resultList)
			{
				del.EndInvoke(iAsyncResult);
				iAsyncResult.AsyncWaitHandle.Close();
			}
		}

		internal delegate void InvokeAsync<T>(IEnumerator<T> enumerator, Action<T> achtion, T item, object syncRoot, int i);

		static void InvokeAction<T>(IEnumerator<T> enumerator, Action<T> action, T item, object syncRoot, int i)
		{
			if (String.IsNullOrEmpty(Thread.CurrentThread.Name))
				Thread.CurrentThread.Name =
			String.Format("Parallel.ForEach Worker Thread No:{0}", i);

			bool moveNext = true;

			while (moveNext)
			{
				action.Invoke(item);

				lock (syncRoot)
				{
					moveNext = enumerator.MoveNext();
					item = enumerator.Current;
				}
			}
		}

		/// <summary>
		/// Parallel for loop. Invokes given action, passing arguments 
		/// fromInclusive - toExclusive on multiple threads.
		/// Returns when loop finished.
		/// </summary>
		/// <remarks>Source: http://coding-time.blogspot.com/2008/03/implement-your-own-parallelfor-in-c.html</remarks>
		public static void For(int fromInclusive, int toExclusive, ForDelegate action)
		{
			// ChunkSize = 1 makes items to be processed in order.
			// Bigger chunk size should reduce lock waiting time and thus
			// increase paralelism.
			int chunkSize = 4;

			// number of process() threads
			int threadCount = Environment.ProcessorCount;
			int cnt = fromInclusive - chunkSize;

			// processing function
			// takes next chunk and processes it using action
			ThreadDelegate process = delegate()
			{
				while (true)
				{
					int cntMem = 0;
					lock (typeof(Parallel))
					{
						// take next chunk
						cnt += chunkSize;
						cntMem = cnt;
					}
					// process chunk
					// here items can come out of order if chunkSize > 1
					for (int i = cntMem; i < cntMem + chunkSize; ++i)
					{
						if (i >= toExclusive) return;
						action(i);
					}
				}
			};

			// launch process() threads
			IAsyncResult[] asyncResults = new IAsyncResult[threadCount];
			for (int i = 0; i < threadCount; ++i)
			{
				asyncResults[i] = process.BeginInvoke(null, null);
			}
			// wait for all threads to complete
			for (int i = 0; i < threadCount; ++i)
			{
				process.EndInvoke(asyncResults[i]);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rudi Breedenraedt
Architect Wolters Kluwer Belgium
Belgium Belgium
Rudi is a Software Architect at Wolters Kluwer Belgium.

| Advertise | Privacy | Mobile
Web02 | 2.8.140821.2 | Last Updated 15 Mar 2010
Article Copyright 2010 by Rudi Breedenraedt
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid