3

I have to start several instances of the application and I have to identify it later somehow.

Is it possible to start instance with some custom TAG so later I can find the instance I need?

Process p = new Process();
// p.TAG = "Guid"; ??????
p.StartInfo.FileName =  "path";
p.Start();

UPDATE 1

I start several processes in the LOAD event of WPF app. And sometimes cannot get process Id of the current app. So I am looking for approach to differentiate an instance in some custom way if it is possible...

So the model looks like

ParentApp.exe
    ---->  Sun.exe
    ---->  Moon.exe


ParentApp.exe
    ---->  Sun.exe
    ---->  Moon.exe

And I use LOAD event of ParentApp.exe to pass to Sun.exe and Moon.exe correct Process ID so later they can able create correct MSMQ and exchange data with ParentApp.exe and also ParentApp.exe could close those apps.

UPDATE 2

May we keep some custom data during application executing here

p.Domain = "mydomain?????";
p.EnvironmentVariables.Add("MY_NEW_VARIABLE", "SOME_TEXT????");

http://blog.gapotchenko.com/eazfuscator.net/reading-environment-variables

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • Look at this: http://stackoverflow.com/questions/611094/async-process-start-and-wait-for-it-to-finish?rq=1 The variable that gets returned from after launching the process, you can store it in a list and then use it to determine if that process is still running. – Versatile Sep 06 '16 at 14:07
  • @Versatile How is this related to the question? – Panagiotis Kanavos Sep 06 '16 at 14:08
  • @Dimi why do you want some other identifier apart from the `Process` object itself? Unless you reuse the same object to start multiple processes, each `Process` object corresponds to the process it started. You can get the process' PID is returned by the Id property – Panagiotis Kanavos Sep 06 '16 at 14:11
  • @Dimi what are you trying to do? Why do you want to attach some tag to a process? Isn't its unique ID enough? – Panagiotis Kanavos Sep 06 '16 at 14:13
  • Another workaround is that Process has Id property. You can access that if it solves your purpose – Versatile Sep 06 '16 at 14:14
  • @PanagiotisKanavos The main problem of this approach that SOMETIMES it doesn't return the Id so I am looking for another approach. If it is possible of course.. – NoWar Sep 06 '16 at 14:14
  • @Dimi define "sometimes". All processes have an ID, there is no other way to identify them. If your process starts a child process and terminates though, you won't get an ID simply because the process has already terminated. The same happens if the process hasn't started *yet*. – Panagiotis Kanavos Sep 06 '16 at 14:16
  • @Dimi What do you want to achieve? Want to setup a communication between them? Or specify a certain task for it? – Jeroen van Langen Sep 06 '16 at 14:16
  • @Versatile why? You *already* have that object, you don't need anything more to identify it! Besides, it's far easier to add the object to a dictionary or store it in a Tuple with a string label that create a derived class – Panagiotis Kanavos Sep 06 '16 at 14:18
  • The essence of this question is still missing.....Why do you need a `Tag`? What does the `Tag` might solve? – Jeroen van Langen Sep 06 '16 at 14:21
  • @PanagiotisKanavos that's why I deleted the comment as soon as I added it because I realized at that very moment while typing the idea :) – Versatile Sep 06 '16 at 14:24
  • @PanagiotisKanavos Take a look at my updated question pls – NoWar Sep 06 '16 at 14:25
  • @JeroenvanLangen Take a look at my updated question pls – NoWar Sep 06 '16 at 14:25
  • In other words, you want to tag the *MSMQ messages* or queues, not the processes. Using a process ID for this is a bad idea. Each application can use its own queue, you can store a different name in configuration, or even pass the queue name through a command line parameter. – Panagiotis Kanavos Sep 06 '16 at 14:31
  • @PanagiotisKanavos Not at all... Everything works just fine but when I try to use Task Scheduler to run ParentApp.exe I am getting weird exception about Process Id... So I need to find a problem. My best Idea is to apply to any dougther app some custom attribute if it is possible hahah ... If it does not work than well.. Let's forget about it... – NoWar Sep 06 '16 at 14:35
  • 3
    Man you guys completely missed the point of this question and the arguing about "why" he needs to tag processes is not helpful. I run a manager where I want to be able to query processes, even after my service restarts. I don't always have the id, and I want my service manager to be stateless. Storing Process Ids is not an option. There, that is my problem, so stop saying to use the process Id that you've already been told is not an option. – Austin Salgat Aug 27 '18 at 16:55
  • Interested to know whether you ever solved this? I’m in a similar boat (process spawns a child process, so can’t track the pid, and its name won’t be unique). At the moment my best idea is to temporarily copy the executable to add a UID so that the MainWindowTitle is unique... – Phueal Aug 21 '20 at 16:25

1 Answers1

1

You could store the Process.Id

Process p = Process.Start("notepad.exe");
MessageBox.Show(p.Id.ToString());
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
  • You don't need to store the Id when you have the process object itself. – Panagiotis Kanavos Sep 06 '16 at 14:12
  • The main problem of this approach that SOMETIMES it doesn't return the Id so I am looking for another approach. If it is possible of course... – NoWar Sep 06 '16 at 14:13
  • @Dimi what do you mean "sometimes"? If your process terminates, or it spawns another process then terminates, the ID will be invalid anyway. There is no other way to identify a process except its ID – Panagiotis Kanavos Sep 06 '16 at 14:14
  • @PanagiotisKanavos I start several process in the LOAD event of wpf app. And sometimes cannot get process Id of the current app. – NoWar Sep 06 '16 at 14:18
  • @Dimi first, why start multiple processes at all? If you want parallel processing, use PLINQ or Dataflow. Second, are you sure the processes have started? You have to wait for them to actually start before checking the ID. In any case, you *don't* even need the Id if you have the object. Finally, you can retrieve all running instances eg with `Process.GetProcessesByName("somename")` – Panagiotis Kanavos Sep 06 '16 at 14:21
  • @PanagiotisKanavos I know I can use `Process.GetProcessesByName("somename")` but not all process have to be associated with the parent process. – NoWar Sep 06 '16 at 14:22