Virtual Developer Workshop: Containerized Development with Docker
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.
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.
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.
Here is a nice exercise on working with threads.
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.
This is just one example on how to create a single instance application though Visual Basic.