Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a process name in C#

Tags:

c#

process

I write an application that starts a 3rd party program that downloads a media stream from a tv channel on my program. The program I am using is rtmpdump.exe. They run as independent processes like this:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = path + rtmpdump;
startInfo.Arguments = rtmpdump_argument;
startInfo.WorkingDirectory = path;
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process p = Process.Start(startInfo);

But I would like to name these processes, so if my program crashes or has to be restarted, then I can check the names of the current processes to see if for example there is one called "BBC World - News at 7". Something like that to identify which has already been started and is currently running. Is it possible? I can't find a way to set a friendly name.

like image 419
Kasper Hansen Avatar asked Sep 13 '13 17:09

Kasper Hansen


People also ask

How do you name a process in C#?

You can't change process names, but instead you can: use Process.Id to identify processes (Id: "... system-generated unique identifier of the process...") get process command line and see if there anything interesting (may need some PInvoke / WMI for that - How to read command line arguments of another process in C#?).

How do I find a process by name?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column. Click on any column name to sort.

How do I change the name of a program in Linux?

To rename a file in the terminal, move the file with mv from itself to itself with a new name.


2 Answers

Extending what Alexei said - you could create a Dictionary< int, string > to keep track of the process ID / descriptive name that you create. Maybe you should write this out to a file in case your program crashes - but you'd need some special startup handling to deal with processes exiting.

On startup you'd want to read in the file, and check current processes to see if they match with what you have, and remove any processes that no longer exist (wrong process id or exe name). You might want to do that every time you create a new process and write to the file.

like image 191
Derek Avatar answered Oct 12 '22 13:10

Derek


You can't change process names, but instead you can:

  • use Process.Id to identify processes (Id: "...system-generated unique identifier of the process...")
  • get process command line and see if there anything interesting (may need some PInvoke / WMI for that - How to read command line arguments of another process in C#?).
like image 29
Alexei Levenkov Avatar answered Oct 12 '22 13:10

Alexei Levenkov