VB Code
If App.PrevInstance Then
MsgBox "Attention! an other copy of this software is running.", vbExclamation + vbOKOnly
End
End If
Public Shared Sub Main()
' refuse to start multiple times on a system
If Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length > 1 Then
Dim result As Integer = MessageBox.Show ("Another Instance of " & Process.GetCurrentProcess.ProcessName & " isalready running ! " & vbCrLf & "This is a server component and needs to run only once ", vbCrLf & "are you sure you want to start another instance ? ", MessageBoxButtons.YesNo,MessageBoxIcon.Question)
If result = 7 Then
Application.Exit()
End If
VB.NET Code
app.previnstance in vb and same in vb.net
In Visual Basic 6 you could use App.PrevInstance to see if your application was already running then issue the End statement to limit your application to a single instance.
In VB.NET you need to get the name of your main module and look for it in the collection of currently running processes. There are a number of ways to do this. Here is one way:
Imports System.Diagnostics
Dim aModuleName As tring=Diagnostics.Process.GetCurrentProcess.MainModule.ModuleName
Dim aProcName As String = System.IO.Path.GetFileNameWithoutExtension(aModuleName)
If Process.GetProcessesByName(aProcName).Length > 1 Then
Application.Exit()
End If
沒有留言:
發佈留言