7

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.

Kasper Hansen
  • 6,307
  • 21
  • 70
  • 106
  • Are you trying to do this so the process displays different names as running processes in windows, or do you just need to have an identifier internally? – JNYRanger Sep 13 '13 at 17:20

5 Answers5

3

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.

Derek
  • 7,615
  • 5
  • 33
  • 58
2

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

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
2

Here is some code that you could use.

public class ProcessTracker {
    public Dictionary<int, string> Processes { get; set; }

    public ProcessTracker() {
        Processes = new Dictionary<int, string>();
    }

    public void AddProcess(Process process, string name) {
        Processes.Add(process.Id, name);
    }

    //Check if what processes are still open after crash.
    public void UpdateProcesses() {
        List<Process> runningProcesses =
            Process.GetProcesses().ToList();

        Processes = Processes
            .Where(pair => runningProcesses
                .Any(process => process.Id == pair.Key))
            .ToDictionary(pair => pair.Key, pair => pair.Value);
    }

    //Use this to see if you have to restart a process.
    public bool HasProcess(string name) {
        return Processes.Any(pair => pair.Value != name);
    }

    //Write the file on crash.
    public void ReadFile(string path) {
        if (!(new FileInfo(path).Exists))
            return;

        using (StreamReader reader = new StreamReader(path)) {
            foreach (string line in  reader.ReadToEnd()
                .Split(new[] {"\n"}, StringSplitOptions.RemoveEmptyEntries)) {
                string[] keyPair = line.Split(',');

                Processes.Add(int.Parse(keyPair[0]), keyPair[1]);
            }
        }
    }

    //Read the file on startup.
    public void SaveFile(string path) {
        using (StreamWriter writer = new StreamWriter(path, false)) {
            foreach (KeyValuePair<int, string> process in Processes) {
                writer.WriteLine("{0},{1}",
                    process.Key, process.Value);
            }
        }
    }
}
Ryan T. Grimm
  • 1,307
  • 1
  • 9
  • 17
1

Writing the process information to a file (or a memory mapped file) might be the way to go;

However, you could also look into windows messaging. i.e. send a message to each process; and have them reply with their "internal name".

Basically you have everything listen in on that queue; You first send a command that orders the other process to report their internal name, which they then dump into the message queue along with their own process id;

See MSMQ

Meirion Hughes
  • 24,994
  • 12
  • 71
  • 122
0

I had the same question to myself, for my solution created a new class - MyProcess which inherits from Process and added a property - ProcessId.

In my case I store the ProcessId on a db which is acossiated to other data. Based on the status of the other data I decide to kill the process.

Then I store process on a dictionary which can be passed to other classes if needed where can be access to kill a process.

So when I decide to kill a process, I pull the process from the dictionary by ProcessId and then kill it.

Sample below:

public class Program
{
    private static Dictionary<string, MyProcess> _processes;

    private static void Main()
    {
        // can store this to file or db
        var processId = Guid.NewGuid().ToString();

        var myProcess = new MyProcess
        {
            StartInfo = { FileName = @"C:\HelloWorld.exe" },
            ProcessId = processId
        };

        _processes = new Dictionary<string, MyProcess> {{processId, myProcess}};

        myProcess.Start();

        Thread.Sleep(5000);

        // read id from file or db or another
        var pr = _processes[processId];

        pr.Kill();

        Console.ReadKey();
    }
}

public class MyProcess : Process
{
    public string ProcessId { get; set; }
}
Vergil C.
  • 1,046
  • 2
  • 15
  • 28
  • Your comment is misleading, I have just tried it it still works, either provide explanation why you think it does not work or remove it. – Vergil C. Nov 15 '20 at 14:46
  • If I open windows task manager, the process name is not set based on the 'ProcessID' string – user1034912 Nov 15 '20 at 15:12
  • 1
    The exact solution that OP is after cannot be achieved, hence alternatives are provided in the answers. My solution is an alternative and I personally use it. Each time your app spins up a new process you record its id in a key value pair, which maybe associated with other data. You can have other apps monitoring each process preiodically, based on their status, the may decide to spin up a new one. So instead of looking at task manager for names, you can look at the ouput (status of spawn processes) of another app for names you have given. Then act accordingly. Hope that helps. – Vergil C. Nov 15 '20 at 17:09
  • Thank you, my mistake. I thought it should update the Task Manager as well – user1034912 Nov 16 '20 at 01:21