在VB.NET中如何让系统中只能有一个EXE程序的实例在运行?

在VB6中,通过APP对象可以方便的判断同一程序的其它实例是否存在。
请问在VB.NET中,没有了原来VB中的APP对象,我如何让系统中只能有一个EXE程序的实例在运行?
---------------------------------------------------------------

讨论过好多次了:P
[VB.NET]

Public Shared Function RunningInstance() As Process

Dim current As Process = Process.GetCurrentProcess()

Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)

'Loop through the running processes in with the same name

Dim process As Process

For Each process In processes

'Ignore the current process

If process.Id <> current.Id Then

'Make sure that the process is running from the exe file.

If [Assembly].GetExecutingAssembly().Location.Replace("/", "") = current.MainModule.FileName Then

'Return the other process instance.

Return process

End If

End If

Next process

'No other instance was found, return null.

Return Nothing

End Function 'RunningInstance

http://www.syncfusion.com/faq/winforms/search/550.asp
---------------------------------------------------------------

'******************************************************************
' 函数名: IsInstanceRunning
' 功 能: 判断工程是否已运行
' 参 数: 无
' 返回值: true 已运行 false 未运行
'******************************************************************
Public Function IsInstanceRunning() As Boolean
Dim current As Process = System.Diagnostics.Process.GetCurrentProcess()
Dim processes As Process() = System.Diagnostics.Process.GetProcessesByName(current.ProcessName)
'Loop through the running processes in with the same name
Dim p As Process
For Each p In processes
'Ignore the current process
If p.Id <> current.Id Then
'Make sure that the process is running from the exe file.
If System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "") = current.MainModule.FileName Then
'Return the other process instance.
Return True
End If
End If
Next
'No other instance was found, return null.
Return False
End Function 'RunningInstance

---------------------------------------------------------------

讨论过好多次了:P
-------------------------------------------------------------------
在程序开始的时候,调用下面这个函数,看返回是不是为空. 这个可以避免程序同名的问题(使用process.ID)

Public Shared Function RunningInstance() As Process

Dim current As Process = Process.GetCurrentProcess()

Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)

'Loop through the running processes in with the same name

Dim process As Process

For Each process In processes

'Ignore the current process

If process.Id <> current.Id Then

'Make sure that the process is running from the exe file.

If [Assembly].GetExecutingAssembly().Location.Replace("/", "") = current.MainModule.FileName Then

'Return the other process instance.

Return process

End If

End If

Next process

'No other instance was found, return null.

Return Nothing

End Function 'RunningInstance

引用于:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c40c.asp#q550q
------------------------------------------------------------
PrevInstance() As Boolean
If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function
相同程序名===error

-----------------------------------------------------------------
芬儿!上酸菜!

Published At
Categories with Web编程
Tagged with
comments powered by Disqus