Search code examples
c#comparisonarraylist

Compare two ArrayList Contents using c#


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.


Solution

  • 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.