Creating Single Instance Applications: Working with Mutexes

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Applications are our bread and butter. There are essentially two instances in which an application can be run: Single Instance and Multiple Instances.

Single Instance Applications

Single Instance Applications means that only one instance can be run at any given point in time. In other words, this means that you cannot run the same application simultaneously.

Multiple Instance Applications

A Multiple Instance Application can be run simultaneously; for example, Notepad. You can have as many instances of Notepad running at any given time as you want.

Mutexes

According to MSDN, you can use a Mutex object to provide exclusive access to a resource. The Mutex class uses more system resources than the Monitor class, but it can be marshaled across application domain boundaries, it can be used with multiple waits, and it can be used to synchronize threads in different processes.

Semaphores

According to Wikipedia, a semaphore is a variable or abstract data type that is used for controlling access, by multiple processes, to a common resource in a concurrent system such as a multiprogramming operating system.

A trivial semaphore is a plain variable that is changed (for example, incremented or decremented, or toggled) depending on programmer-defined conditions. The variable then is used as a condition to control access to some system resource.

Threading

Here is a nice exercise on working with threads.

Our Application

Start Visual Basic and create a new Console application. Add the following code to your application:

Imports System.Threading
Module Module1

   Sub Main()
      Dim OneMutex As Mutex = Nothing
      Const MutexName As String = "RUNMEONLYONCE"

      Try
         OneMutex = Mutex.OpenExisting(MutexName)
      Catch ex As WaitHandleCannotBeOpenedException

      End Try

      If OneMutex Is Nothing Then
         OneMutex = New Mutex(True, MutexName)
      Else
         OneMutex.Close()
         Return
      End If

      Console.WriteLine("Our Application")
      Console.Read()


   End Sub

End Module

A Mutex object gets created and then later on we check whether this Mutex is currently in use. If it is in use, we exit; otherwise, we run the application normally.

Conclusion

This is just one example on how to create a single instance application though Visual Basic.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read