Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo

  How to run only once (Thursday, January 27, 2005)




Found the following interesting discussion in the Newsgroups:

Run only once
by:Anonymous

Hello,

I want to make an aplication that runs only one time.

Let me explain better, each time I click on 'calculator' icon, it will open several 'calculators'.

What I want to do is to make a aplication that, if I click is icon it will open the aplication, if the aplication is already open it will 'bring it to front' instead of open a new aplication.
Thank's in advance
Luis Mendes


 Reply:
by:Cor Ligthert

 Hi Luis,

When you speak a little bit German I would look here, it gives the best
solutions.
http://dotnet.mvps.org/dotnet/code/application/

"Nur eine etc.

Without the German you can have a look at the code and if that not succeed
as well than

Single instance
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/reaworapps1.asp
I hope this helps a little bit?

Cor


 Reply:
by:Greg Burns

 Here is the code I am using (although it does not bring the "running
instance to the front", sounds like a good idea).

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


 Reply:
by:Andre Nogueira

 The code I currently use is

Dim Processes() As Process
Processos = Process.GetProcessesByName("Taskfind")
If Processos.Length > 1 Then
End
'If any process with the same name is running, end the application
End If
Application.Run(FormToBeRun) 'If not, open the form

This code is in Sub Main.

Andre Nogueira


 Reply:
by:hirf-spam-me-here@gmx.at (Herfried K. Wagner [MVP])

 *
<URL:http://www.pobox.com/~skeet/csharp/faq/#one.application.instance>

--
Herfried K. Wagner [MVP]








0 Comments:

Post a Comment

 
Previous Posts
    - OutOfMemoryException
    - How to transfer values bewteen Integer and UInt32 ...
    - How to transfer values between Integer and UInt32 ...
    - How to set file persmissions with WMI using a UNC ...
    - Week date
    - How to set a dataset date value to null
    - If only MSDN examples would actually work!
    - How to use managed code in Office 2003
    - Click Event not fired
    - How to read a particular setting from a file

Archives
    - 10/03/2004 - 10/10/2004
    - 10/10/2004 - 10/17/2004
    - 10/17/2004 - 10/24/2004
    - 10/24/2004 - 10/31/2004
    - 10/31/2004 - 11/07/2004
    - 11/21/2004 - 11/28/2004
    - 11/28/2004 - 12/05/2004
    - 12/05/2004 - 12/12/2004
    - 12/12/2004 - 12/19/2004
    - 12/19/2004 - 12/26/2004
    - 12/26/2004 - 01/02/2005
    - 01/23/2005 - 01/30/2005
    - 01/01/2006 - 01/08/2006
    - 09/24/2006 - 10/01/2006

Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland