I have an agent program that will launch multiple instance of the same executable. Each of those instances need to have an individual ID associated with them.
The agent retains a reference to the Process object that was used to load the instance, but I have to consider that the agent may be shut down and restarted without affecting the started instances.
Once the agent starts again, I need it to search the existing processes and rebind a reference to the processes.
How can I assign data to a process and retrieve it afterwards?
Right now, I am starting the process like this:
this.AttachedProcess = new Process()
{
StartInfo = new ProcessStartInfo(filename)
};
And, later, I need to search for that process by calling Process.GetProcesses().
While I could use a command line argument to start the process (something like -instance XX) and read that command line using this answer, I’d like to know if there is another way to assign extra data to a process and retrieve it later.
You could save the Process.Id of the processes you create in a file.
Upon startup you read that file and check if those processes still are started and check that the filename matches (if the system has been restarted some other processes might have got those ids)