[RESOLVED] How would it be possible to...
How would it be possible to close all instances of my program at the same time?
Say if someone clicks "exit" on one instance of my program, how could i make it also close all other instances?
The real question here is it possible to do this directly? Without writing to a file or the registry & have the other program constantly reading that location?
Would it be possible to have a program "listen" for a command, or when somthing is called in one program it's also called in all the others, i think this is possible with classes but it's been a while.
Re: How would it be possible to...
I suppose you could attempt to use the FindWindow() API function to search for any instances of the application and close them. Ill try figure some other things out to :)
Re: How would it be possible to...
Yeah, ok will do.
What's VB's class name? ThunderRT6Main?
I'm thinking something like this:
Code:
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
Private Sub Close_All()
Dim Index As Long
Dim Closer_hWnd As Long
Dim Closer_Hdle As Long
Dim sOldCaption As String
sOldCaption = Me.Caption
Me.Caption = "Closing..."
For Index = 0 To 500
Closer_hWnd = FindWindow("ThunderRT6Main", Me.Caption)
If Closer_hWnd <= 1 Then GoTo Final_Close
Closer_Hdle = GetWindow(Closer_hWnd, GW_HWNDPREV)
PostMessage Closer_Hdle, WM_CLOSE, 0, 0
PostMessage Closer_Hdle, WM_QUIT, 0, 0
Next Index
Final_Close:
Me.Caption = sOldCaption
Closer_hWnd = FindWindow("ThunderRT6Main", Me.Caption)
If Closer_hWnd <= 1 Then MsgBox "Error, some instances may still be open"
Closer_Hdle = GetWindow(Closer_hWnd, GW_HWNDPREV)
PostMessage Closer_Hdle, WM_CLOSE, 0, 0
PostMessage Closer_Hdle, WM_QUIT, 0, 0
Unload Me
End
End Sub
The thing is however, it might close itself before all the other ones are closed (since it might not be the last instance that opened up calling it)?
*** Edit: Actually, i think i fixed that by changing the caption.
Looks good?
Re: How would it be possible to...
I would be cheap, disgusting and hacky in this one myself.
vb Code:
AppActivate Me.Caption
SendKeys "%{F4}" 'Gag @ SendKeys
Then just trap the error that pops up when the window isn't found, means no more exist.
Re: How would it be possible to...
SendKeys is very inefficient i am completely against it. And won't that shut down all applications ?
Re: How would it be possible to...
Nah, just all instances of his application... Unless all programs have the same window title as his.
Yes, I hate SendKeys, hence the comment in that code.
But my god, I am so lazy sometimes.
Re: How would it be possible to...
Re: How would it be possible to...