在VB.Net中启动进程时,我想给它一个自定义名称,我可以将此进程作为参数使用的任何函数都可以访问它。
我以这种方式启动流程:
Dim mainProcessHandler As New ProcessStartInfo()
Dim mainProcess As Process
mainProcessHandler.FileName = "something_01out18.bat"
mainProcessHandler.Arguments = "-d"
mainProcessHandler.WindowStyle = ProcessWindowStyle.Hidden
mainProcess = Process.Start(mainProcessHandler)
如果我什么也不做,使用时
mainProcess.ProcessName
我将得到“ cmd”,因为它是由cmd运行的蝙蝠脚本。
我可以做类似的事情
mainProcess.myCustomName = "bat01out18"
并在函数中调用
Sub doThingsWithProcess(ByVal usedForThingsProcess As Process) As Boolean
infoConsoleDisplay("process " + usedForThingsProcess.myCustomName + " will be used to for things")
End Sub
我很确定有办法实现这一目标,但也许可以采用不同的方法。你有什么主意吗?
答案 0 :(得分:-1)
您可以创建一个继承自Process
的子类,然后可以向其中添加所需的任何自定义属性。这是一个示例:
Public Class CustomProcess
Inherits Process
Public Property DisplayName As String
End Class
用法:
Dim pInfo As New ProcessStartInfo()
pInfo.FileName = "cmd.exe"
pInfo.Arguments = "/C ping 127.0.0.1"
pInfo.WindowStyle = ProcessWindowStyle.Normal
Dim mainProcess As New CustomProcess() With {.DisplayName = "MyProcess"}
mainProcess.StartInfo = pInfo
mainProcess.Start()
Console.WriteLine($"Process '{mainProcess.DisplayName}' will be doing so and so.")
输出:
Process 'MyProcess' will be doing so and so.