By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,854 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,854 IT Pros & Developers. It's quick & easy.

How to prevent a second instance of an application.

P: n/a
Hi
How do I prevent a program from beeing started more than one time.
IE. If a dot net app is already started how do I prevent a second instance
to be started ?

Tnx
Totto
Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Totto,

Take from this the one which fits you the best.
http://tinyurl.com/3gdgr

I assume this will help?

Cor
Nov 20 '05 #2

P: n/a
Option Strict On
Imports System.Threading

Module Main
Private Sub SubMain()

SingletonApp.Run(New MyForm)

End Sub
Enc Module
Public Class SingletonApp

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form)
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean

' use this to create a unique guid for your app...
'Dim g As New Guid
'g = Guid.NewGuid
'Debug.WriteLine(g.ToString)

m_Mutex = New Mutex(False, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4") '
arbitrary GUID
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function

Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex()
m_Mutex.Close()
End Sub
End Class

HTH,
Greg

"Totto" <ta********@norcon.no> wrote in message
news:Ox**************@tk2msftngp13.phx.gbl...
Hi
How do I prevent a program from beeing started more than one time.
IE. If a dot net app is already started how do I prevent a second instance
to be started ?

Tnx
Totto

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.