Month: April 2013

How to cancel Parallel.ForEach

static void Main(string[] args) { List<RandomWait> waiters = new List<RandomWait>(); for (int i = 0; i < 1000; i++) { waiters.Add(new RandomWait()); } int counter = 0; ParallelOptions options = new ParallelOptions(); CancellationTokenSource cancelToken = new CancellationTokenSource(); options.MaxDegreeOfParallelism = 15; options.CancellationToken = cancelToken.Token; Task.Factory.StartNew(() => { Parallel.ForEach(waiters, options, (waiter) => { counter++; Console.WriteLine(counter); waiter.Pause(); counter–; Console.WriteLine(counter); }); }); Console.ReadLine(); cancelToken.Cancel();… Read more →

Roll-your-own lazy loading

Thanks to @dot_NET_Junkie for pointing me to this article How *not* to inject services into entities Here’s my version: public interface IEntityLoader<T> where T : class { bool IsLoaded { get; } T Value { get; } } /// /// This class has been designed to enable Lazy Loading within an Entity /// without the need for the Entity to hold a… Read more →

Aspect Oriented Programming (AOP)

We have found many programming problems for which neither procedural nor object-oriented programming techniques are sufficient to clearly capture some of the important design decisions the program must implement. This forces the implementation of those design decisions to be scattered throughout the code, resulting in “tangled” code that is excessively difficult to develop and maintain. We present an analysis of why certain design… Read more →