0

When starting a process in VB.Net, I would like to give it a custom name that can be accessed by any function I give this process to as a argument.

I launch my process this way :

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)

If I do nothing more, when using

mainProcess.ProcessName

I will get "cmd" as it's a bat script run by cmd.

Can I do something like

mainProcess.myCustomName = "bat01out18"

And call it in a function

Sub doThingsWithProcess(ByVal usedForThingsProcess As Process) As Boolean
    infoConsoleDisplay("process " + usedForThingsProcess.myCustomName + " will be used to for things")
End Sub

I'm pretty sure there is a way to achieve such thing but maybe with a different approach. Do you have any idea ?

Julien D
  • 25
  • 6

1 Answers1

-1

You can create a subclass that inherits from Process, then you can add any custom properties you like to it. Here's an example:

Public Class CustomProcess
    Inherits Process

    Public Property DisplayName As String

End Class

Usage:

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.")

Output:

Process 'MyProcess' will be doing so and so.
  • That solved my issue. I set a mainProcess.DisplayName = "Process doing that" before each mainProcess.StartInfo = mainProcessStartInfo mainProcess.Start() – Julien D Sep 05 '18 at 07:52
  • @JulienD Exactly, or you can use object initializer (i.e., `With {.DisplayName = "MyProcess"}`) as shown in the answer. – 41686d6564 stands w. Palestine Sep 05 '18 at 15:10
  • Sure but as I use the same mainProcess instance several times for different bat scripts, I need to overwrite the DisplayName over time. Thank you. – Julien D Sep 06 '18 at 08:57