PDA

Click to See Complete Forum and Search --> : Multiple instances of an app...


Phailak
Feb 5th, 2001, 07:42 AM
Hi,

We use this application which stays open in the task manager when you close it with the X button
of the window instead of the exit button from the file menu. Sometimes we find up to 10 instances of the same application opened in the task manager because they were closed with the X button. What I need to
know is, can I use FindWindow or something to determine if the app is already running? The name of the
window is "Multilink SRB", I want to close the app as soon as two or more of them are present in the task
manager, but so far, I only get the handle of the first one???

Any suggestions?

Phailak

Rh0ads
Feb 5th, 2001, 07:48 AM
i don't fully understand the question , but i think that you can go :

app.previnstance

on the form load and i think that will stop multiple copys of it from being loaded

Phailak
Feb 5th, 2001, 08:36 AM
No I can't do that because it's not my app I'm trying to avoid having more than one instance, but I found a solution, thanx anyway

Phailak

Rh0ads
Feb 5th, 2001, 09:15 AM
Now i get it, sorry for the confusion

Chris
Feb 7th, 2001, 12:50 AM
Phailak: here is how you have only 1 instances of your application running all the time.


'//Put this code under the BAsic Module File
'//And Set the Main() as your entire program
'//start up function.
'//And Assume you Application Main Form is call frmMain.

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private xhwnd As Long

Public Sub Main()
Dim xAppName As String
If App.PrevInstance Then
xAppName = "Multilink SRB"
xhwnd = FindWindow(0&, xAppName)
If xhwnd <> 0 Then
AppActivate xAppName
xhwnd = ShowWindow(xhwnd, SW_SHOWNORMAL)
End
End If
End If

Load frmMain
frmMain.Show
End Sub


you can download the sample code from my home page WIN32API | pre_instance.zip (http://skyscraper.fortunecity.com/data/845/)

Phailak
Feb 7th, 2001, 05:50 AM
Thanx Chris

Phailak

Chris
Feb 7th, 2001, 08:17 AM
Well, I'm happy too 'coz my sample code does help you :)

shragel
Feb 7th, 2001, 09:56 PM
The real solution is stopping the problem to begitm with.

You should unload all forms loaded before terminating the view from your app.
in example.
[code]
private sub Form_Unload
call Module1.ExitAll
End Sub

'In Module1

Public Sub ExitAll ()
For Each Form In Forms
'You can even create a test if any forms are still visible
if Form.Visible = False Then
unload Form
Else
End if
Next
End Sub

JordanChris
Feb 15th, 2001, 04:57 AM
The code discussed here (and in many other places) always closes the latest (just started) version of the application, and switches to a previous running copy.

I need to do the opposite!
Keep the current application running, but delete the earlier copy.

Any idea how to do that??


Thanks, Chris

And if you ask why?
The latest copy will have been started with a Right Click in Windows Explorer, and use of the SendTo my application. This will pass in COMMAND() with the names of the files I need to work on. If I kill the current app I lose this list of files, so I need to kill the previous one.

JordanChris
Feb 15th, 2001, 10:55 AM
Solved:

The code:
<code:>

Private Sub Form_Load()
Dim AppName, x
Dim hWindow As Long

If App.PrevInstance Then
AppName = App.Title
App.Title = "Nothing"
hWindow = FindWindow(vbNullString, AppName)
App.Title = AppName
If hWindow <> 0 Then
x = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
x = WaitForSingleObject(hWindow, INFINITE)
End If
End If
Command2.Caption = "Close the other"
End Sub

And the declarations:

Private Declare Function WaitForSingleObject Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Private Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Private Declare Function PostMessage Lib "user32" _
Alias "PostMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long

'Constants used by the API functions
Const WM_CLOSE = &H10
Const INFINITE = &HFFFFFFFF

</code:>

I think thats it.
It changes the new apps window title, finds another app with the original title and kills it, and then sets this apps title back again.

Cheers, Chris