Introduction
I needed to implement a single and limited instance application as a part of my project. I searched a lot on the internet, but it proved to be like finding a chocolate inside an egg. Mainly, this problem was about synchronizing access to the resources used by the application. After searching on MSDN, I found a way to do so simply as well as effectively. The solution I managed to develop involves use of operating system level objects, say, kernel objects.
This article explains about these objects, which I used in my application to get my job done. These objects are, namely:
Mutex
Semaphore
Of course, there is one more kernel object that I would like to mention here, which is an Event
object. We can use Event
objects to notify multiple threads across application domains, but I'm not going to discuss them in this article.
Background
As a starting point, I found some clues from the article published on this site about single instance applications.
Using the Code
Single Instance Applications
First, I am going to show you how we can create a single instance application using the Mutex
class provided in .NET Framework 2.0. This object is mainly used to lock data and resources across application domains and process boundaries. When we create a Mutex
object in our application, it is assigned a unique handle by the kernel of our operating system. The following is the code to create a single instance application:
Dim mt As Mutex = Nothing
Const Name As String = "MyMutex"
Try
mt = Mutex. OpenExisting(Name)
Catch e As WaitHandleCannotBeOpenedException
Catch ex As Exception
End Try
If mt Is Nothing Then
mt = New Mutex(True, Name)
Else
mt. Close()
Return
End If
By using the OpenExisting()
method of the Mutex
object, you can check for the existence of any Mutex
object with the same name. Note that this method will throw an exception of "No handle of the given name exists" if there is no Mutex
object with the given name existing. So, it's better to handle this exception in our code.
Limited Instance Application
To create a limited instance application, you need to allow access to resources to the set number of threads in the same or across application domains or process domains. In such situations, our Semaphore
kernel object comes into the picture. This object uses a limited number of slots for resources at once. For the sake of our application, we consider a slot as an instance of our application. When these slots reach the maximum number, remaining instances of the application will not be allowed to run. Using the following code, you can create a maximum of 3 application instances at the same time:
Dim Sem As Semaphore = Nothing
Const Name As String = "MySemaphore"
Try
Sem = Semaphore. OpenExisting(Name)
Catch e As WaitHandleCannotBeOpenedException
Catch ex As Exception
End Try
If Sem Is Nothing Then
Sem = New Semaphore(1, 5, Name)
Else
Sem. Release()
Return
End If
To free a slot, you can use the Release()
method of the Semaphore
object. Additionally, you can specify the number of slots to release in this method. When this method is used without parameters, it means that it will release one slot per instance of an application. Based on the currently running number of instances, you can check for the instance termination. The rest of the thing is the same as in a Mutex
object, like checking for an existing Semaphore
with named Semaphore
and exception handling.
Points of Interest
Even though these objects are helpful in creating single and limited instance applications, they are mainly used for thread synchronization across application and process domains. One more thing that I must mention is that these objects are kernel mode objects, so they come with overhead. I mean that they are heavyweight objects. So, I suggest you use them while keeping your eye on the performance of your application and the functionality you need to achieve.
History
- 14 September, 2007 -- Original version posted
Tejas has been an ASP.NET,AJAX/C#.NET/VB.NET developer for about 10 years for windows, WCF, RESTful services, windows and web application. Currently, he is working on Distributed Applications as a team lead in his company. He has worked with languages like C,C++,Java,PHP and databases like MS SQL Server, Oracle and MySQL. He especially enjoy web development using service driven architectures. He holds a MCSD certification since July 2013.
In his free time, he likes to learn new technologies including real world business processes, contribute to online forums/blogs, listen to music and hang out with friends.