|
-
Feb 5th, 2001, 08:42 AM
#1
Thread Starter
Addicted Member
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
-
Feb 5th, 2001, 08:48 AM
#2
Addicted Member
Verifying
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
-
Feb 5th, 2001, 09:36 AM
#3
Thread Starter
Addicted Member
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
-
Feb 5th, 2001, 10:15 AM
#4
Addicted Member
Oh
Now i get it, sorry for the confusion
-
Feb 7th, 2001, 01:50 AM
#5
PowerPoster
Phailak: here is how you have only 1 instances of your application running all the time.
Code:
'//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
Last edited by Chris; Feb 7th, 2001 at 02:15 AM.
-
Feb 7th, 2001, 06:50 AM
#6
Thread Starter
Addicted Member
-
Feb 7th, 2001, 09:17 AM
#7
PowerPoster
Well, I'm happy too 'coz my sample code does help you
-
Feb 7th, 2001, 10:56 PM
#8
Frenzied Member
Unload Form
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
-
Feb 15th, 2001, 05:57 AM
#9
Frenzied Member
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.
-
Feb 15th, 2001, 11:55 AM
#10
Frenzied Member
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
Last edited by JordanChris; Feb 15th, 2001 at 12:06 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|