使用 运行 实例在 vb.net 中执行命令行
Use running instance to execute commandline in vb.net
我想将我的应用程序的 运行ning 实例(单个实例应用程序)用于 运行 新命令行...
我听说过互斥锁和IPC机制,但我不知道如何使用它。
解释:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(Environment.CommandLine)
End Sub
End Class
示例:
我用一个文件作为参数启动了应用程序,它显示了 MsgBox,我让它 运行。如果我再次以文件作为参数启动应用程序,它不会显示 MsgBox ...
如何使用新命令行显示它?
问候,Drarig29。
在 VB.NET 中,您可以从项目属性页面将您的应用程序设为单一实例。勾选"Make single instance application"选项,然后点击"View Application Events"按钮:
在 ApplicationEvents.vb
class 中,为 StartupNextInstance
添加处理程序 - 当应用程序已经 运行 并且您再次启动它时将调用它。您可以在主窗体上调用方法:
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
' Handle arguments when app is already running
If e.CommandLine.Count > 0 Then
' Pass the argument to the main form
Dim form = TryCast(My.Application.MainForm, Form1)
form.LoadFile(e.CommandLine(0))
End If
End Sub
End Class
End Namespace
在您的主窗体中,您可以传递初始命令行参数,并使用常用方法处理后续参数:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
' Handle arguments from the initial launch
Dim args = Environment.GetCommandLineArgs()
If args.Length > 1 Then
LoadFile(args(1))
End If
End Sub
Public Sub LoadFile(filename As String)
MessageBox.Show(filename)
End Sub
End Class
我想将我的应用程序的 运行ning 实例(单个实例应用程序)用于 运行 新命令行... 我听说过互斥锁和IPC机制,但我不知道如何使用它。
解释:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(Environment.CommandLine)
End Sub
End Class
示例:
我用一个文件作为参数启动了应用程序,它显示了 MsgBox,我让它 运行。如果我再次以文件作为参数启动应用程序,它不会显示 MsgBox ... 如何使用新命令行显示它?
问候,Drarig29。
在 VB.NET 中,您可以从项目属性页面将您的应用程序设为单一实例。勾选"Make single instance application"选项,然后点击"View Application Events"按钮:
在 ApplicationEvents.vb
class 中,为 StartupNextInstance
添加处理程序 - 当应用程序已经 运行 并且您再次启动它时将调用它。您可以在主窗体上调用方法:
Namespace My
Partial Friend Class MyApplication
Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
' Handle arguments when app is already running
If e.CommandLine.Count > 0 Then
' Pass the argument to the main form
Dim form = TryCast(My.Application.MainForm, Form1)
form.LoadFile(e.CommandLine(0))
End If
End Sub
End Class
End Namespace
在您的主窗体中,您可以传递初始命令行参数,并使用常用方法处理后续参数:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
' Handle arguments from the initial launch
Dim args = Environment.GetCommandLineArgs()
If args.Length > 1 Then
LoadFile(args(1))
End If
End Sub
Public Sub LoadFile(filename As String)
MessageBox.Show(filename)
End Sub
End Class