Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I need to ensure that application should not run twice.If attempt it should return to main form.
Posted

The easiest way to do this is to use a Mutex to tell if another instance is already running. There's surely tons of articles online about this ?
 
Share this answer
 
I was making Player in vb WPF and just this worked for me. It closes other instances. Hope it helps you. Just convert to C# :)

VB
Imports System.Threading

Private Shared appGuid As String = "c0a76b5a-12ab-45c5-b9d9-d693faa6e7b9" 'Your App Unique Guid
    Private Shared mutex As Mutex
    Public Sub New()
        InitializeComponent()
        Dim mutexCreated As Boolean

        mutex = New Mutex(True, "Global\" + appGuid, mutexCreated)
        If mutexCreated Then
            mutex.ReleaseMutex()
        End If

        If Not mutexCreated Then
            'App is already running, close this!
            Environment.[Exit](0)
        End If
    End Sub
 
Share this answer
 
v3
Comments
CHill60 21-Apr-15 11:11am    
I rather hope that the OP found the links in Solution 2 - which also use Mutex and are already in C#. They've had over 3 years to consider them after all.
Beware - Answering old questions tends to attract down-votes - especially if the underlying solution has already been proposed or if the new post is in the wrong language.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900