vb.net 怎么判断当前程序是否正在运行?
是Vb.net,不是VB第一次运行后,再次双击exe文件,不再开始运行新的程序,而是打开已经运行的exe程序的窗口。...
是Vb.net, 不是VB
第一次运行后,再次双击exe文件,不再开始运行新的程序,而是打开已经运行的exe程序的窗口。 展开
第一次运行后,再次双击exe文件,不再开始运行新的程序,而是打开已经运行的exe程序的窗口。 展开
2个回答
展开全部
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
<DllImport("User32.dll")>
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
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
<DllImport("User32.dll")>
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询