4

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.

2 Answers 2

7

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());
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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 ?
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.
3

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.

2 Comments

Can u tell me how to print the Missing and new processes in messagebox ? Thanks.
It depends on what your environment is and what you use for GUI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.