|
-
Feb 3rd, 2004, 08:27 PM
#1
Thread Starter
Fanatic Member
PrevInstance kills FIRST instance
I want just one instance of my app running at a time. When a second instance of my app occurs, I need to have the app start from scratch. I can't just set focus to the first instance and kill the second instance.
My app is only run when called from another program. Certain events in that other program cause it to set some data and then run my app. My app then gets the data and does its thing. If the user doesn't close my app when they are done, the other program will keep running new instances. If a second instances occurs, I want to kill one instance. But, whichever instance of my app that I keep running needs to go get the new data. So, if there is a PrevInstance, I need to either:
1) Kill the first instance and let the second instance run and load the new data.
or
2) Kill the second instance and somehow get the first instance to load the new data. I can't find any Form events (Initialize, Load, Activate, GotFocus, etc.) that I can cause to happen from the second event (especially considering that the new data shuld only be loaded by the program and never the user. So I'm thinking I should go with option 1, but I'm not sure how to kill the first instance of the app?
-
Feb 3rd, 2004, 09:08 PM
#2
G'day WorkHorse,
This is a simplistic approach (I modified this previous post of Marty's):
VB Code:
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const GW_HWNDPREV = 3
Const WM_CLOSE = &H10
Const WM_QUIT = &H12
Private Sub Form_Load()
If App.PrevInstance Then
TerminatePrevInstance
End If
End Sub
Sub TerminatePrevInstance()
Dim sOldTitle As String
Dim lPrevHndl As Long
'Save the title of the application.
sOldTitle = App.Title
'Rename the title of this application so FindWindow
'will not find this application instance.
App.Title = "unwanted instance"
'Attempt to get window handle using VB4 class name.
lPrevHndl = FindWindow("ThunderRTMain", sOldTitle)
'Check for no success.
If lPrevHndl = 0 Then
'Attempt to get window handle using VB5 class name.
lPrevHndl = FindWindow("ThunderRT5Main", sOldTitle)
End If
'Check if found
If lPrevHndl = 0 Then
'Attempt to get window handle using VB6 class name
lPrevHndl = FindWindow("ThunderRT6Main", sOldTitle)
End If
'Check if found
If lPrevHndl = 0 Then
'No previous instance found, restore App Title
App.Title = sOldTitle
Exit Sub
End If
'Get handle to previous window.
lPrevHndl = GetWindow(lPrevHndl, GW_HWNDPREV)
PostMessage lPrevHndl, WM_CLOSE, 0, 0
PostMessage lPrevHndl, WM_QUIT, 0, 0
End Sub
I havn't tested it 
Bruce.
Last edited by Bruce Fox; Feb 3rd, 2004 at 09:11 PM.
-
Feb 3rd, 2004, 09:29 PM
#3
Thread Starter
Fanatic Member
Thanks. That will do the trick. I expected I'd have to do something like that. I don't see any reason to not restore the app title whether or not the hWnd of the previous instance has been found, so I adjusted that to keep the proper app name in task manager. Works great.
-
Feb 3rd, 2004, 11:12 PM
#4
Glad I could help
-
Feb 3rd, 2004, 11:13 PM
#5
Thread Starter
Fanatic Member
Originally posted by MartinLiss
Glad I could help
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
|