Newsgroup: it.comp.lang.visual-basic
Titolo: [?]Negare l'avvio dello stesso programma più volte

Come impedire l'avvio dello stesso programma piů volte?
Grazie!

 Grazie!
all'inizio del programma:
if App.PrevInstance = True then
  ' Ho un'altra istanza del mio programma attiva
Else
  ' Sono la prima istanza avviata
End If
Bye by Massimo 8-]
P.S. Guardati l'help, per vedere se ha il comportamento che ti
aspetti...

 That's it! Hope this helps.
Attribute VB_Name = "basStartup"
Option Explicit
Private Const SW_RESTORE    As Long = &H9&
Private Const SW_SHOW       As Long = &H5&
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA"
(ByVal hWndParent As Long, ByVal hWndChildAfter As Long, ByVal lpszClass
As String, ByVal lpszWindow As String) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowText Lib "user32" Alias
"GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal
cch As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As
Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As
Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long,
ByVal nCmdShow As Long) As Long
Private Declare Function WinSetFocus Lib "user32" Alias "SetFocus"
(ByVal hWnd As Long) As Long
Public Sub Main()
'The entire program starts here.
    'If this program is already running in memory, then show the one
    'that's already running and end this one, otherwise run this one.
    'Replace "Previous Instance Demo" with your main form's caption.
    'This can be a partial caption, but should be unique on the desktop.
    If ShowPrevInstance("Previous Instance Demo") Then
        'Previous instance is running, so end
        End
    Else
        'Show the main form
        'Replace frmDemo with your main form's name
        frmDemo.Show
    End If
End Sub
Private Function ShowPrevInstance(ByVal sPartialTitle As String) As
Boolean
'Shows a Previous Instance of this program, if one exists in memory.
'sPartialTitle = the caption of your main form and
'should be unique among the top-level Desktop windows.
Dim lRet        As Long
Dim hWndPrev    As Long
    If App.PrevInstance Then
        'Previous instance running, find it
        hWndPrev = FindPrevInstance(sPartialTitle)
        If hWndPrev <> 0 Then
            'Show the window
            If IsIconic(hWndPrev) <> 0 Then
                'Restore to normal, if minimized...
                lRet = ShowWindow(hWndPrev, SW_RESTORE)
            Else
                '...Otherwise, just show it
                lRet = ShowWindow(hWndPrev, SW_SHOW)
            End If
            'Bring it to the front and set focus to it
            lRet = SetForegroundWindow(hWndPrev)
            lRet = WinSetFocus(hWndPrev)
        End If
        ShowPrevInstance = True
    Else
        ShowPrevInstance = False
    End If
End Function
Private Function FindPrevInstance(ByVal sPartialTitle As String) As Long
'Finds a Previous Instance of this program in memory, if one exists.
'sPartialTitle = the caption of your main form and
'should be unique among the top-level Desktop windows.
Dim hWndDeskTop As Long
Dim hWndChild   As Long
Dim lChars      As Long
Dim sCaption    As String
Dim sClass      As String * 50
Dim sClassName  As String
    'Start from the Desktop...
    hWndDeskTop = GetDesktopWindow()
    If hWndDeskTop > 0 Then
        'Search through all top-level windows
        hWndChild = FindWindowEx(hWndDeskTop, 0, vbNullString,
vbNullString)
        While hWndChild <> 0
            'Get the window's caption
            sCaption = String$(1024, " ")
            lChars = GetWindowText(hWndChild, sCaption, Len(sCaption))
            If lChars > 0 Then
                sCaption = Left$(sCaption, lChars)
            Else
                sCaption = ""
            End If
            'Does it match?
            If InStr(1, LCase$(Trim$(sCaption)), LCase$(sPartialTitle))
= 1 Then
                'Yes...Found the window
                FindPrevInstance = hWndChild
                hWndChild = 0
            Else
                'No...Check the next one
                hWndChild = FindWindowEx(hWndDeskTop, hWndChild,
vbNullString, vbNullString)
            End If
        Wend
    End If
End Function