简体   繁体   中英

C# - Finding difference between two lists of Process type

I am trying to compare the two process lists and get only the difference (the newly started processes). So when I click a button in my winform, the program should load the oldProcesses, then load the newProcesses, compare them in Difference and show Difference list. The Difference list on the first time should be empty because oldProcesses and newProcesses are same. But it does not happen, the problem is with this code: var Difference = newProcesses.Except(oldProcesses).ToList(); How can I get the difference between newProcesses and oldProcesses? Thank you

    Process[] oldProcesses = { };
    int clicked = 0;

    private void button1_Click_1(object sender, EventArgs e)
    {           
        if (clicked == 0)
        {
            oldProcesses = Process.GetProcesses();            
            clicked = 1;
        }
        Console.WriteLine("Old: " + oldProcesses.Count());

        Process[] newProcesses = Process.GetProcesses();
        Console.WriteLine("New: " + newProcesses.Count());

        var Difference = newProcesses.Except(oldProcesses).ToList();
        Console.WriteLine("Diff: " + Difference.Count());


        string allproc = "";
        foreach(Process theprocess in oldProcesses)
        {
            allproc += theprocess.ProcessName.ToString() +"\n";
        }
        MessageBox.Show(allproc);

        allproc = "";
        foreach (Process theprocess in newProcesses)
        {
            allproc += theprocess.ProcessName.ToString() + "\n";
        }
        MessageBox.Show(allproc);


        allproc = "";
        foreach (Process theprocess in Difference)
        {
            allproc += theprocess.ProcessName.ToString() + "\n";
        }
        MessageBox.Show(allproc);


        /*Process[] processlist = Process.GetProcesses();
        string ProcList = "";
        string temp;
        foreach (Process theprocess in processlist)
        {
            temp = theprocess.ProcessName.ToString() + "\n";
            ProcList += temp;
        }*/
    }
public class ProcessDiff : IEqualityComparer<Process>
{

    public IEnumerable<Process> GetDiff(IEnumerable<Process> oldProcesses, IEnumerable<Process> newProcesses )
    {
        return newProcesses.Except(oldProcesses, this);
    }
    public bool Equals(Process x, Process y)
    {
        if (x == null && y == null) return true;
        if (x == null || y == null) return false;
        return x.Id == y.Id && x.ProcessName == y.ProcessName && x.SessionId == y.SessionId;
    }

    public int GetHashCode(Process obj)
    {
        if (obj == null) return 0;
        return obj.Id;
    }
}

And Usage:

        var oldProc = Process.GetProcesses();
        var newProc = Process.GetProcesses();

        var diff = new ProcessDiff().GetDiff(oldProc, newProc);

You actually don't need to make a separate class for this if you don't want to. You could also just make the form implement IEqualityComparer<Process> and add the required Equals() / GetHashCode() methods to it. The Except method has a parameter for the class instance that contains the comparison method ( this if it's called in the same class containing the IEqualityComparer methods).

Updated: I added some additional constraints because Process.Id can be recycled. You may want to check for other factors depending on what your needs are here. Keep in mind that an Idle process will throw exceptions on certain properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM