+44 (0)1179 113711

Rock Solid Knowledge Blogs

An aggregation of all the Rock Solid Knowledge Blogs

Subscribe  Subscribe

Slides and Demos from BASTA!

Thanks to everyone who came to my sessions at BASTA! My first time at that conference and I had a great time. Here are the slides and demos

What’s New in Workflow 4.0

Contract First Development with WCF

Generics, Extension Methods and Lambdas – Beyond List<T>

Posted 26/09/2009 Richard Blewett

Microsoft Mouse Preventing Screen Saver Working

I've just installed Windows 2008 R2 and I could not get the screen saver or power management to work. Turns out I needed to install the correct drivers for my mouse. This is the Wireless Laser 5000, the drivers can be found here.
Posted 18/08/2009

Fluent Parallel While

During devweek 2009 Oliver introduced me to Fluent Api's, I personally love programs that naturally read, after all programs are read far more often than written.  This week a posting on the Parallel Extensions blog demonstrated how to achieve parallel while, since we only have Parallel.For and Parallel.ForEach

 

static void Main(string[] args)
{
ConcurrentQueue<int> queue = new ConcurrentQueue<int>();

for (int i = 0; i < 10; i++)
{
queue.Enqueue(i);
}


Action<ParallelLoopState> processQueue = (lps) =>
{
int item;
if (queue.TryDequeue(out item))
{
Console.WriteLine(" Thread Id = {0} Task Id = {1} : {2}", Thread.CurrentThread.ManagedThreadId, Task.Current.Id, item);
}
};

Func<bool> queueContainsItems = () => queue.IsEmpty == false;

ParallelUtil.While(new ParallelOptions(), queueContainsItems, processQueue);
}





I thought I’d combine both these ideas and produce what I would consider a more  Fluent version




public static class ParallelUtil
{
private static IEnumerable<bool> Infinite()
{
while (true) yield return true;
}

private static ParallelOptions NoParallelOptions = new ParallelOptions();

public static void InParallelWhile(this Action<ParallelLoopState> action, Func<bool> condition, ParallelOptions options = null)
{
if (options == null) options = NoParallelOptions;

Parallel.ForEach(IterateForever(), options ,
(ignored, loopState) =>
{
if (!condition())
{
loopState.Stop();
}
else
{
action(loopState);
}
});

}

private static IEnumerable<bool> IterateForever()
{
while (true)
{
yield return true;
}
}
}
class Program
{
static void Main(string[] args)
{
ConcurrentQueue<int> queue = new ConcurrentQueue<int>();

for (int i = 0; i < 10; i++)
{
queue.Enqueue(i);
}


Action<ParallelLoopState> processQueue = (lps) =>
{
int item;
if (queue.TryDequeue(out item))
{
Console.WriteLine(" Thread Id = {0} Task Id = {1} : {2}", Thread.CurrentThread.ManagedThreadId, Task.Current.Id, item);
}
};

Func<bool> queueContainsItems = () => queue.IsEmpty == false;

processQueue.InParallelWhile(queueContainsItems);
}
}
}









The key difference is the first example you use the following line to process the queue in parallel



ParallelUtil.While(new ParallelOptions(), queueContainsItems, processQueue);



As opposed to the second version with a perhaps more fluent implementation



processQueue.InParallelWhile(queueContainsItems);

Posted 14/08/2009 noreply@blogger.com (Andy Clymer)