Este artículo se tradujo automáticamente. Para ver el artículo en inglés, active la casilla Inglés. Además, puede mostrar el texto en inglés en una ventana emergente si mueve el puntero del mouse sobre el texto.
Traducción
Inglés

Método Process.Close ()

 

Publicado: octubre de 2016

Libera todos los recursos asociados a este componente.

Espacio de nombres:   System.Diagnostics
Ensamblado:  System (en System.dll)

public void Close()

The M:System.Diagnostics.Process.Close method causes the process to stop waiting for exit if it was waiting, closes the process handle, and clears process-specific properties. M:System.Diagnostics.Process.Close does not close the standard output, input, and error readers and writers in case they are being referenced externally.

System_CAPS_noteNota

The M:System.Diagnostics.Process.Dispose(System.Boolean) method calls M:System.Diagnostics.Process.Close. Placing the T:System.Diagnostics.Process object in a using block disposes of resources without the need to call M:System.Diagnostics.Process.Close.

The following example starts an instance of Notepad. It then retrieves the physical memory usage of the associated process at 2-second intervals for a maximum of 10 seconds. The example detects whether the process exits before 10 seconds have elapsed. The example closes the process if it is still running after 10 seconds.


using System;
using System.Diagnostics;
using System.Threading;

namespace Process_Sample
{
   class MyProcessClass
   {
      public static void Main()
      {
         try
         {
            Process myProcess;
            myProcess = Process.Start("Notepad.exe");
            // Display physical memory usage 5 times at intervals of 2 seconds.
            for (int i = 0;i < 5; i++)
            {
               if (!myProcess.HasExited)
               {
                   // Discard cached information about the process.
                   myProcess.Refresh();
                   // Print working set to console.
                   Console.WriteLine("Physical Memory Usage: " 
                                        + myProcess.WorkingSet.ToString());
                   // Wait 2 seconds.
                   Thread.Sleep(2000);
               }
               else {
                   break;
               } 
            }

            // Close process by sending a close message to its main window.
            myProcess.CloseMainWindow();
            // Free resources associated with process.
            myProcess.Close();

         }
         catch(Exception e)
         {
            Console.WriteLine("The following exception was raised: ");
            Console.WriteLine(e.Message);
         }
      }
   }
}

LinkDemand

for full trust for the immediate caller. This member cannot be used by partially trusted code.

.NET Framework
Disponible desde 1.1
Volver al principio
Mostrar: