在vb裡面可以勾選『單一執行個體』,但是小弟在Server 2008 R2 X64 某些客戶機器底下,發現這個功能完全破功,只好用手動的在程式裡面加上功能
在Form load裡面
Dim intTimes As Integer
Dim strMeName() As String
Dim strName As String
intTimes = 0
'取出名字,包涵檔名路徑
strMeName = Split(Application.ExecutablePath, "\")
'真正的名字是在最後一個
strName = strMeName(strMeName.Length - 1).ToUpper
'不要.exe結尾
strName = Split(strName, ".EXE")(0)
'偵測有無執行過了
For Each p As Process In Process.GetProcesses()
Try
For Each m As ProcessModule In p.Modules
If Mid(m.ModuleName, 1, strName.Length).ToUpper = strName Then
intTimes = intTimes + 1
End If
Next
Catch ex As Exception
End Try
Next
Try
'如果有兩個人,就離開
If intTimes > 1 Then
'自殺
'離開並關閉執行緒
Environment.Exit(Environment.ExitCode)
Application.Exit()
End If
Catch ex As Exception
End Try
這樣無論OS版本為何,幾個User登入,都可以確保只有一隻程式在跑
沒有留言:
張貼留言