Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2005, 10:15
SOLAR SOLAR is offline
Friend
 
Join Date: Aug 2004
Posts: 126
Rept. Given: 6
Rept. Rcvd 2 Times in 2 Posts
Thanks Given: 12
Thanks Rcvd at 6 Times in 6 Posts
SOLAR Reputation: 2
How to?-Prevent multiple instance of an application

I am writin the app in vb 6.0 and vb.net and want to know how to prevent the application from being opened multiple times on the same computer.

i.e. If the application is already open clicking on the app's icon will only bring the currently open application to the front instead of opening another instance of the app.

Thank you for all help given.
Reply With Quote
  #2  
Old 05-24-2005, 12:26
just4urim
 
Posts: n/a
One of the best and easiest way to prevent ur app. from running again when its run , is to create a named Mutex for ur app. then you should check it at the begining of ur app. and if CreateMutex returns the ERROR_ALREADY_EXISTS , then you can be sure that another instance of ur app is running and do whtevere u want ! (Find more info about the "CreateMutex" API in MSDN)
Reply With Quote
  #3  
Old 05-24-2005, 14:11
oVERfLOW oVERfLOW is offline
Family
 
Join Date: Jan 2005
Location: Tehran
Posts: 117
Rept. Given: 127
Rept. Rcvd 42 Times in 19 Posts
Thanks Given: 1
Thanks Rcvd at 5 Times in 5 Posts
oVERfLOW Reputation: 42
I prefer to use FindWindowEx API to find a similar window
You can find your window properties by SPY++

I think that it's more useful because your process may be killed and the user won't be able to launch your app by the reboot.

ba bye
Reply With Quote
  #4  
Old 05-24-2005, 15:22
surferxyz surferxyz is offline
Friend
 
Join Date: Jan 2005
Location: Planet Earth
Posts: 73
Rept. Given: 0
Rept. Rcvd 9 Times in 4 Posts
Thanks Given: 10
Thanks Rcvd at 52 Times in 19 Posts
surferxyz Reputation: 9
VB.Net

Code:
            '#Zone " Prevent multiple instances "
            If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
                'Send opening form's TEXT property as a parameter to the function "ActivatePrevInstance"
                Dim PrevHndl As Long
                Dim result As Long
                Dim argStrAppToFind As String = "XXXXXXXXXXXXXXXX"
                'Variable to hold individual Process
                Dim objProcess As New Process
                'Collection of all the Processes running on local machine
                Dim objProcesses() As Process
                ''Get all processes into the collection
                objProcesses = Process.GetProcesses()

                For Each objProcess In objProcesses
                    ''Check and exit if we have SMS running already
                    If objProcess.MainWindowTitle.Length > 9 Or objProcess.MainWindowTitle = "xxxxxxxxxxxxxxxxxxxx" Then

                        If objProcess.MainWindowTitle = "xxxxxxxxxxxxxxxxxxxxxx" Then
                            MsgBox("Another instance of " & argStrAppToFind & " is already running on this machine. You cannot run TWO instances at a time. Please use the other instance.", MsgBoxStyle.Exclamation)
                            PrevHndl = objProcess.MainWindowHandle.ToInt32()
                            Exit For
                        End If
                        If (UCase(objProcess.MainWindowTitle.Substring(0, 12)) = UCase(argStrAppToFind)) Then
                            MsgBox("Another instance of " & argStrAppToFind & " is already running on this machine. You cannot run TWO instances at a time. Please use the other instance.", MsgBoxStyle.Exclamation)
                            PrevHndl = objProcess.MainWindowHandle.ToInt32()
                            Exit For
                        End If

                    End If
                Next
                If PrevHndl = 0 Then Exit Sub 'if No previous instance found exit the application.
                ''If found
                result = OpenIcon(PrevHndl) 'Restore the program.
                result = SetForegroundWindow(PrevHndl) 'Activate the application.

                End 'End the current instance of the application.
            End If
            '#End Zone
Reply With Quote
  #5  
Old 05-24-2005, 16:44
goggles99 goggles99 is offline
Friend
 
Join Date: Aug 2004
Posts: 62
Rept. Given: 5
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 1
Thanks Rcvd at 4 Times in 4 Posts
goggles99 Reputation: 0
Arrow

hmm, for vb6 it can be done very simply using "App.PrevInstance".

Code:
Private Form_Load( )
     If App.PrevInstance Then
          Unload Me
     End If
End Sub
for vb.net it's a bit different, but this function should work
Code:
Function PrevInstance() As Boolean
If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess).ProcessName)) > 0 Then
     Return True 
Else 
     Return False  
End If
End Function
There is also a similar way to do it in vb.net here
http://www.thescarms.com/dotNet/SingleInstance.asp


Last edited by goggles99; 05-24-2005 at 16:47.
Reply With Quote
  #6  
Old 05-24-2005, 22:39
redbull redbull is offline
Friend
 
Join Date: Mar 2004
Posts: 160
Rept. Given: 17
Rept. Rcvd 5 Times in 4 Posts
Thanks Given: 3
Thanks Rcvd at 6 Times in 6 Posts
redbull Reputation: 5
if you use MUTEXes as suggested by "just4urim", be sure to release your mutex using a call to releaseMutex() before your program quits
Reply With Quote
  #7  
Old 05-24-2005, 23:37
SOLAR SOLAR is offline
Friend
 
Join Date: Aug 2004
Posts: 126
Rept. Given: 6
Rept. Rcvd 2 Times in 2 Posts
Thanks Given: 12
Thanks Rcvd at 6 Times in 6 Posts
SOLAR Reputation: 2
Great! Thank you for all the help. I will report back once ive tested it
Reply With Quote
  #8  
Old 05-25-2005, 01:58
tAz
 
Posts: n/a
in addition, you'll need to find the previously running app (findwindow), and make it the active window before you exit your program (use win messages). this way, running a 2nd instance of your app brings up the first instance.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[C] NoDel (tool to prevent file deletion) Insid3Code Source Code 0 02-10-2015 16:38
Prevent browser from being killed nino General Discussion 4 01-10-2014 02:38


All times are GMT +8. The time now is 22:31.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( 1998 - 2024 )