views:

118

answers:

2

I have two arraylist. Namely ExistingProcess and CurrentProcess.

The ExistingProcess arraylist contains the list of process that was running when this application started.

The CurrentProcess arraylist is in a thread to fetch the process running in the system all the time.

Each time the currentProcess arraylist gets the current process running, i want to do a compare with ExistingProcess arraylist and show in a messagebox like,

Missing process : NotePad [If notepad is closed and the application is launched] New Process : MsPaint [if MSPaint is launched after the application is launched]

Basically this is a comparison of two arraylist to find out the new process started and process closed after my c# application is launched.

Hope my question is clear. Help Needed.

+3  A: 

First, go over the first list and remove each item from the second list. Then vice versa.

    var copyOfExisting = new ArrayList( ExistingProcess );
    var copyOfCurrent = new ArrayList( CurrentProcess );

    foreach( var p in ExistingProcess ) copyOfCurrent.Remove( p );
    foreach( var p in CurrentProcess ) copyOfExisting.Remove( p );

After that, the first list will contain all the missing processes, and the second - all new processes.

Fyodor Soikin
@Fyodor Soikin, Can u tell me how to print the Missing and new processes in messagebox ? Thanks.
Anuya
It depends on what your environment is and what you use for GUI.
Fyodor Soikin
+5  A: 

You could use LINQ Except.

Except produces the set difference of two sequences.

For samples:

http://msdn.microsoft.com/en-us/library/bb300779.aspx

http://msdn.microsoft.com/en-us/library/bb397894%28VS.90%29.aspx

Code to illustrate the idea...

static void Main(string[] args)
{
    ArrayList existingProcesses = new ArrayList();

    existingProcesses.Add("SuperUser.exe");
    existingProcesses.Add("ServerFault.exe");
    existingProcesses.Add("StackApps.exe");
    existingProcesses.Add("StackOverflow.exe");

    ArrayList currentProcesses = new ArrayList();

    currentProcesses.Add("Games.exe");
    currentProcesses.Add("ServerFault.exe");
    currentProcesses.Add("StackApps.exe");
    currentProcesses.Add("StackOverflow.exe");

    // Here only SuperUser.exe is the difference... it was closed.   
    var closedProcesses = existingProcesses.ToArray().
                          Except(currentProcesses.ToArray());

    // Here only Games.exe is the difference... it's a new process.   
    var newProcesses = currentProcesses.ToArray().
                       Except(existingProcesses.ToArray());
}
Leniel Macaferi
@Leniel Macaferi, Your code matches the content to its exact array place. But i want to check then with each other. Because when i compare with process name, the order may be different.
Anuya
@Leniel Macaferi, As a result of implementing your code, it shows the everything as new process and everything as Closed process. Because it checks one to one. It should not be the case. Right ?
Anuya
The order of the items won't affect the result. From what I understood this is exactly what you need. It'll give you two lists: 1 for closed processes and 1 for new processes.
Leniel Macaferi