8

Here is the piece of code, which run through all the process and when It finds the right process, code sends the message. My question is what happened to the 'proc', how to dispose that process.

//get all other (possible) running instances
        Process[] processes = Process.GetProcesses();            
        foreach (Process proc in processes)
        {
            if (proc.ProcessName.ToLower() == ProcessName.ToLower())
            {
                SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
            }               
        }

Thanks in advance, Harsha

Harsha
  • 1,861
  • 7
  • 28
  • 56
  • 1
    You can be explicit about case insensitive string comparisons with the `Equals` method: `if (proc.ProcessName.Equals(ProcessName, StringComparison.OrdinalIgnoreCase))` – Phil Gan Feb 23 '11 at 13:12
  • 1
    see also: http://stackoverflow.com/questions/16957320/what-does-process-dispose-actually-do – eFloh Jan 26 '15 at 17:50

4 Answers4

7

To make sure all resoucers are freed as early as possible, call Dispose on the process, when you no longer need it.

//get all other (possible) running instances
Process[] processes = Process.GetProcesses();
try
{
    foreach (Process proc in processes)
    {
    // use proc
    }
}
finally
{
    foreach (Process proc in processes)
        proc.Dispose();
    processes = null;
}
Henrik
  • 23,186
  • 6
  • 42
  • 92
4

In general terms you don't need to worry about disposing or deallocating objects, unless the object implements the IDisposable interface. If it does you should either call the Dispose() method on it manually when you're finished, or wrap with a using statement to have it called automatically:

using (var disposableObject = new DisposableType())
{
    // do work with disposableObject
}
Phil Gan
  • 2,813
  • 2
  • 29
  • 38
  • although in this case, the code quoted probably isn't marking the end of life for the processes, and so should leave them undisposed. – Massif Feb 23 '11 at 12:33
  • The `using` statement is of little help in this case because `GetProcesses` does all the instantiation. – vgru Feb 22 '13 at 10:00
  • 2
    @Massif: Dipsosing the Process object will NOT close the process, but only your handle to the same. Missing to dispose the handle will waste unmanaged resources until the GC will collect your Process object. – eFloh Jan 26 '15 at 17:47
-2

IF you are looping to find your won process then you could try something like:

Process.GetCurrentProcess();

In any case I would change it to:

    foreach (Process proc in Process.GetProcesses())
    {
        if (proc.ProcessName.ToLower() == ProcessName.ToLower())
        {
            SendMessage(proc.MainWindowHandle, (uint)Message, IntPtr.Zero, IntPtr.Zero);
        }               
    }

That way no variable will refernce the "GetProcesses" and the GC would eventually handle it.

PedroC88
  • 3,708
  • 7
  • 43
  • 77
-3

The variable proc is local to the foreach loop so once the loop completes, it will automatically be garbage collected.

Joe
  • 11,147
  • 7
  • 49
  • 60