vb.net 怎么判断当前程序是否正在运行?

2024-11-30 02:34:00
推荐回答(2个)
回答1:

Imports System.Runtime.InteropServices
Module Module1
Sub Main()
Dim instance As Process = RunningInstance()
If instance IsNot Nothing Then
ShowWindowAsync(instance.MainWindowHandle, 3) '调用api函数,正常显示窗口
Return
End If
Dim F1 As New Form1
Application.Run(F1) '显示窗体
End Sub

Public Function ShowWindowAsync(ByVal hWnd As System.IntPtr, ByVal cmdShow As Integer) As Boolean
End Function
Private Function RunningInstance() As Process '返回进程中已经打开的程序
Dim current As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)
For Each process As Process In processes
If process.Id <> current.Id Then
If process.MainModule.FileName = current.MainModule.FileName Then
Return process
End If
End If
Next
Return Nothing
End Function
End Module

回答2:

Private Sub Form_Load()
If App.PrevInstance = True Then
MsgBox "程序已经在运行", , "工程一"
Unload Me
End
End If
End Sub