Creating Single Instance Applications: Working with Mutexes

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.



About the Author

Hannes DuPreez

Hannes du Preez has been a Microsoft MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978@gmail.com

Related Articles

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date