使用互斥体:'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
在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
If App.PrevInstance = True Then Unload Me exit sub end if 即可!
先使用App.PrevInstance判断,然后再用app.TaskVisible
Private Sub MDIForm_Load() If App.PrevInstance Then '检视前一版本 MsgBox "此程序已经在运行中!", 48 Unload Me End If End Sub这是我用的,其他人没得分的话,就全给我了。
'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
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
Unload Me
exit sub
end if
即可!
MsgBox "此程序已经在运行中!", 48
Unload Me
End If
End Sub这是我用的,其他人没得分的话,就全给我了。