This article describes the operation of a method-based static code analyzer for .NET that constructs in-memory method call networks of compiled assemblies. You will also see a concrete application of static code analysis to generate a website providing easy insights on a sample application.
- codeanalyzerandtodosample.zip
- CodeAnalyzer and Sample Solution.sln
- Generate.cmd
- Generator
- Output
- Blank.html
- co_CreateToDoItem(int,string,Nullable[DateTime])_ToDoItem.html
- co_DeleteToDoItem(int)_void.html
- co_FindUserByName(string)_User.html
- co_GetToDoItem(int)_ToDoItem.html
- co_GetUser(int)_User.html
- co_ListAllToDoItems()_IEnumerable[ToDoItem].html
- co_ListAllUsers()_IEnumerable[User].html
- co_ListDoneItemsForUser(int)_IEnumerable[ToDoItem].html
- co_ListToDoItemsForUser(int)_IEnumerable[ToDoItem].html
- co_ListUsersMatching(string)_IEnumerable[User].html
- co_MarkToDoItemDone(int)_void.html
- co_UpdateToDoItem(int,string,Nullable[DateTime])_void.html
- CodeModel.css
- CodeModel.js
- Content.html
- Index.html
- IndexTop.html
- so_CreateToDoItem(int,string,Nullable[DateTime])_ToDoItem.html
- so_DeleteToDoItem(int)_void.html
- so_FindUserByName(string)_User.html
- so_GetUser(int)_User.html
- so_ListAllUsers()_List[User].html
- so_ListDoneItemsForUser(int)_List[ToDoItem].html
- so_ListToDoItemsForUser(int)_List[ToDoItem].html
- so_ListUsersMatching(string)_List[User].html
- so_MarkToDoItemDone(int)_void.html
- so_UpdateToDoItem(int,string,Nullable[DateTime])_ToDoItem.html
- ui_CreateNewToDoItemClicked(object,System.Windows.RoutedEventArgs)_void.html
- ui_DeleteClicked(object,System.Windows.RoutedEventArgs)_void.html
- ui_InitializeComponent()_void.html
- ui_MarkDoneClicked(object,System.Windows.RoutedEventArgs)_void.html
- ui_RefreshToDoItems()_void.html
- ui_SaveClicked(object,System.Windows.RoutedEventArgs)_void.html
- ui_ToDoItemSelected(object,System.Windows.Controls.SelectionChangedEventArgs)_void.html
- ui_UserSelected(object,System.Windows.Controls.SelectionChangedEventArgs)_void.html
- ui_WindowLoaded(object,System.Windows.RoutedEventArgs)_void.html
- SampleApp
- LocalTestRun.testrunconfig
- ToDoApplication
- ToDoContract
- ToDoSample.vsmdi
- ToDoService
- ToDoUnitTests
- StaticCodeAnalyzer
- Arebis.CodeAnalysis.Static
- Arebis.Common
|
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.