.NET · C# · Dynamics CRM 4.0 · Synchronization · Threading

C#.NET – Making your code Thread Safe

Today I had to make my synchronization (MSCRM4.0 – MSSQL) code thread-safe. Thought I would share a few things I learned about this.

The magic statement today is lock.

Lock is C# .NET special statement that marks a block of code as a critical section with mutual-exclusion lock. Follow this link for further read http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx.

An example code without lock:

public class NotThreadSafeProcess
{
	public void StartWork()
	{
		DoWork();
	}

	private void DoWork()
	{
	}
}

An example code with lock:

public class ThreadSafeProcess
{
	static readonly object _locker = new object();

	public void StartWork()
	{
		bool okToProcess = false;

		lock (_locker)
		{
			DoWork();
		}
	}

	private void DoWork()
	{
	}
}

The synchronization function that I was working on is run on a schedule triggered by MSCRM4.0 plugin. The worst case here is that multiple plugin threads invoke the synchronization web service at the same time. And running two or more synchronization threads at the same time is never a good idea. Making synchronization requests queue up is the lesser of the two evils, but isn’t a good solution either, because it will just bog down the system.

 

Therefore, the solution is to make sure that there is one and only one synchronization thread running at a time and to cancel other requests that came in when a synchronization thread is running.

public class SingleThreadSafeProcess
{
	static readonly object _locker = new object();
	static bool isProcessing = false;

	public void StartWork()
	{
		bool okToProcess = false;

		lock (_locker)
		{
			if (!isProcessing)
			{
				isProcessing = true;
				okToProcess = true;
			}
		}

		try
		{
			if (okToProcess)
			{
				DoWork();
			}
		}
		finally
		{
			if (okToProcess)
				isProcessing = false;
		}
	}

	private void DoWork()
	{
	}
}

The code above will only lock the access static variable isProcessing which will indicates whether a process is running at a time; and cancel all other requests if there is.

There are many ways to Rome

There are other ways to do this, eg. with Singleton. Please feel free to share by leaving a comment.


Cheers – Sy

Advertisements

2 thoughts on “C#.NET – Making your code Thread Safe

  1. Have a look at some other primitives also like ReadWriterLocksSlim ect. Monitor is expensive. Also Imutability can help make you design lock free.

    Locks should be used only when no other synchronization techniques or primitives are available.

    Blairhttps://sliong.wordpress.com/2011/02/07/c-net-making-your-code-thread-safe/#comment-form-load-service:Twitter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s