How can I prevent the application from being runned twice?
- i'm using VB 6.0 Enterprise Edition
Printable View
How can I prevent the application from being runned twice?
- i'm using VB 6.0 Enterprise Edition
Code:Private Sub Form_Load()
If App.PrevInstance = True Then End
End Sub
Tut, tut, tut, Megatron. We use:
Nowadays.Code:Unload Me
I forgive you.
This is discussed more at the following threat
http://forums.vb-world.net/showthrea...threadid=26409
Thanks Again!!!
Actually, V(ery) Basic, the correct way is:
Code:Dim frm As Form
For Each frm In Forms
Unload frm
Set frm = Nothing
Next
If another instance of the application is found, and that application was minimized by the user, is it possible to activate that instance and maximize it ?
Thanks.
I think this will work
Code:If App.PrevInstance Then
' Activate the previous instance
AppActivate App.Title
' Send a key (here SHIFT-key) to set the
' form from the previous instance to the
' top of the screen.
SendKeys "+", True
End If
Thanks for the reply. That code hi-lites the minimized application, but does not maximize it.
I'll try some different key combinations and see if anything works.
Once the minimized application is hi-lited, manually performing CTRL-M will maximize the window after I click on the app, but when I put those keys in code the window does not maximize.
I am using: SendKeys ("^M"),True
This works for me:[code]Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, 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_SHOWMAXIMIZED = 3
Private Sub Form_Load()
Dim lhwnd As Long
Dim lRtrn As Long
Dim X As Form
If App.PrevInstance = True Then
'Get Window Handle
lhwnd = FindWindow(vbNullString, App.Title)
'Activate and Maximize Window
lRtrn = ShowWindow(lhwnd, SW_SHOWMAXIMIZED)
'Kill App
For Each X In Forms
Unload X
Set X = Nothing
Next
End If
End Sub
[Edited by WadeD on 08-18-2000 at 11:46 AM]
I'm not sure why, but that code does not maximize the original application for me. It does kill the second instance though.
Thanks for the code. I'll keep experimenting with it.
My mistake...it works if you're doing this for another window like "Untitled - Notepad". If there are multiple instances, it acts on the first window it finds with that caption, which in this case is itself so it stops executing.
[Edited by WadeD on 08-18-2000 at 12:35 PM]
What I gave you previously works for a separate app (like Word or Excel).
This works for killing the second instance of the same app and activating/maximizing the first instance:Code:Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, 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_SHOWMAXIMIZED = 3
Private Sub Form_Load()
Dim lhwnd As Long
Dim lRtrn As Long
Dim sFrmCaptn As String
Dim X As Form
'Get Caption to Search For
sFrmCaptn = Me.Caption
Me.Caption = ""
'Search for Window / Get its Handle
lhwnd = FindWindow(vbNullString, sFrmCaptn)
'If Another Window Found
If lhwnd <> 0 Then
'Activate and Maximize Window
lRtrn = ShowWindow(lhwnd, SW_SHOWMAXIMIZED)
'Kill App
For Each X In Forms
Unload X
Set X = Nothing
Next
Unload Me
'If No Other Window Found
Else
'Set Caption back to Original Caption
Me.Caption = sFrmCaptn
End If
End Sub
I appreciate you taking the time to come up with this code, but it is not working for me. Instead, two instances of the application are open.
I have my code to the point where it will at least inform the user that the application is already running, which should be good enough for now.
I'll save the enhancements for later.
Thanks again.
Hmmm...it's a copy-and-paste directly from a new project in which it works for me. Some API's work differently on different OS's. I'm in Win NT, using VB6 SP4.
I'm using the same OS as you. NT4, SP6 with VB6.0,SP4.
I did a straight copy and paste into my form code, but still no success.
I might experiment with it some early next week.
Thanks.
If I create a totally new project and copy your code in, the results are perfect.
Must be something about the existing project I am trying to add the code to, even though the startup form just contains logon code.
So, your code works great. I'll just need to keep trying.