|
-
Aug 18th, 2000, 09:23 AM
#1
Thread Starter
Lively Member
How can I prevent the application from being runned twice?
- i'm using VB 6.0 Enterprise Edition
-
Aug 18th, 2000, 09:28 AM
#2
Code:
Private Sub Form_Load()
If App.PrevInstance = True Then End
End Sub
-
Aug 18th, 2000, 09:33 AM
#3
Fanatic Member
Tut, tut, tut, Megatron. We use:
Nowadays.
I forgive you.
-
Aug 18th, 2000, 09:34 AM
#4
Fanatic Member
-
Aug 18th, 2000, 09:38 AM
#5
Thread Starter
Lively Member
-
Aug 18th, 2000, 09:48 AM
#6
Actually, V(ery) Basic, the correct way is:
Code:
Dim frm As Form
For Each frm In Forms
Unload frm
Set frm = Nothing
Next
-
Aug 18th, 2000, 10:00 AM
#7
Fanatic Member
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.
-
Aug 18th, 2000, 10:05 AM
#8
_______
<?>
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
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 18th, 2000, 10:22 AM
#9
Fanatic Member
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.
-
Aug 18th, 2000, 10:35 AM
#10
Fanatic Member
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
-
Aug 18th, 2000, 10:43 AM
#11
Hyperactive Member
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]
-
Aug 18th, 2000, 11:03 AM
#12
Fanatic Member
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.
-
Aug 18th, 2000, 11:21 AM
#13
Hyperactive Member
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]
-
Aug 18th, 2000, 12:22 PM
#14
Hyperactive Member
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
-
Aug 18th, 2000, 12:35 PM
#15
Fanatic Member
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.
-
Aug 18th, 2000, 12:46 PM
#16
Hyperactive Member
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.
-
Aug 18th, 2000, 12:55 PM
#17
Fanatic Member
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.
-
Aug 18th, 2000, 01:01 PM
#18
Fanatic Member
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.
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
|