Showing posts with label Multithreaded. Show all posts
Showing posts with label Multithreaded. Show all posts

Monday, 23 January 2012

Concurrency Checker

Useful piece of code when checking for concurrency in your application.

using System;
using System.Threading;

namespace Utilities
{
public class ConcurrencyCheck : IDisposable
{
private static int _concurrencyCount = 0;

public ConcurrencyCheck()
{
var usage = 0;
if ((usage = Interlocked.Increment(ref _concurrencyCount)) != 1)
{
Console.WriteLine("{0} delegates are running concurrently", usage);
}
}

public void Dispose()
{
Interlocked.Decrement(ref _concurrencyCount);
}
}
}