在vb 2008 express中,此选项在应用程序属性下可用.有谁知道它的功能是什么?这样做是为了不可能同时打开两个实例吗?
Ste*_*eve 11
为什么不使用Mutex呢?这是MS建议的,我已经使用它多年,没有任何问题.
Public Class Form1
Private objMutex As System.Threading.Mutex
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Check to prevent running twice
objMutex = New System.Threading.Mutex(False, "MyApplicationName")
If objMutex.WaitOne(0, False) = False Then
objMutex.Close()
objMutex = Nothing
MessageBox.Show("Another instance is already running!")
End
End If
'If you get to this point it's frist instance
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
在这种情况下,表单关闭时,互斥锁将被释放,您可以打开另一个互斥锁.即使你的应用程序崩溃,这也有效.
小智 -3
甚至还有一个更简单的方法:
使用以下代码...
Imports System.IO
Run Code Online (Sandbox Code Playgroud)
在主窗体加载事件上执行以下操作:
If File.Exist(Application.StartupPath & "\abc.txt") Then
'You can change the extension of the file to what ever you desire ex: dll, xyz etc.
MsgBox("Only one Instance of the application is allowed!!!")
Environment.Exit(0)
Else
File.Create(Application.StartupPath & "\abc.txt", 10, Fileoptions.DeleteonClose)
Endif
Run Code Online (Sandbox Code Playgroud)
这将处理单个实例以及瘦客户端,并且在应用程序运行时无法删除该文件。关闭应用程序或应用程序崩溃时,文件将自行删除。
| 归档时间: |
|
| 查看次数: |
32098 次 |
| 最近记录: |