如果我已经使用了app.TaskVisible=False,就不能再用App.PrevInstance判断该程序是否已经运行了,请问有什么办法判断该程序是否已经执行,最好能给出实现代码!

解决方案 »

  1.   

    使用互斥体:'This code must be pasted into a module
    'Set the project's startup object to 'Sub Main' (-> Project -> Project Properties -> General Tab -> Startup Object)
    Public Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
    Public Type SECURITY_ATTRIBUTES
            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long
    End Type
    Public Const ERROR_ALREADY_EXISTS = 183&
    Private Sub Main()
       
        ' -> code by Raist Lin
        Dim sa As SECURITY_ATTRIBUTES
        sa.bInheritHandle = 1
        sa.lpSecurityDescriptor = 0
        sa.nLength = Len(sa)
        'Try to create a new Mutex
        Debug.Print CreateMutex(sa, 1, App.Title)
        Debug.Print Err.LastDllError
        'Check if the function was succesfull
        If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
            'More than one instance detected
            MsgBox "More than one instance"
        Else
            'No other instance detected...
            'Your program-load code here
        End If
    End Sub
      

  2.   

    在form_load中写代码:
    Private Sub Form_Load()
       App.TaskVisible = True
       If App.PrevInstance = True Then
          Unload Me
          App.TaskVisible = False
          Exit Sub
       End If
       App.TaskVisible = False
    End Sub
      

  3.   

    If App.PrevInstance = True Then
          Unload Me
          exit sub
    end if
    即可!
      

  4.   

    先使用App.PrevInstance判断,然后再用app.TaskVisible
      

  5.   

    Private Sub MDIForm_Load()    If App.PrevInstance Then        '检视前一版本
        MsgBox "此程序已经在运行中!", 48
        Unload Me
        End If
    End Sub这是我用的,其他人没得分的话,就全给我了。