Import saved Processes (pid) as ProcessChild class

Viewed 25

Goal is to remember a list of external processes if my application is closed in the meantime. Therefore process pids are saved into a file and imported on application launch. Now the problem arises:

As long as process ids should be imported of type System.Diagnostics.Process there is no problem as I can use Process.GetProcessById:

public List<Process> GetKnownProcesses()
{
    int[] saved_ids = { 1, 2, 3 };
    List<Process> known_processes = new List<Process>();
    for (int id in save_ids)
    {
        known_processes.Add(Process.GetProcessById(id)); //No Exception handling to keep it simple
    }
    return known_processes;
}

However in its current form I have to use a childclass of Process.

public class ProcessChild : Process
{
    //extending some functionality
}

public List<ProcessChild> GetKnownProcesses()
{
    int[] saved_ids = { 1, 2, 3 };
    List<ProcessChild> known_processes = new List<ProcessChild>();
    for (int id in save_ids)
    {
        known_processes.Add(Process.GetProcessById(id)); //ERROR: I would have to Downcast here
    }
    return known_processes;
}

So this clearly won't work as I would have to downcast Process into ProcessChild. Copying properties from parent to child is also of no use, because several Properties are read / get only. Is there an efficient way to resolve this issue, without writting my own GetProcessById method? Maybe there is another way of setting certain properties and calling an update method of some sort?

0 Answers
Related