Process.Close Método

Definición

Libera todos los recursos asociados a este componente.

public:
 void Close();
public void Close();
member this.Close : unit -> unit
Public Sub Close ()

Ejemplos

En el ejemplo siguiente se inicia una instancia del Bloc de notas. A continuación, recupera el uso de memoria física del proceso asociado a intervalos de 2 segundos durante un máximo de 10 segundos. En el ejemplo se detecta si el proceso se cierra antes de transcurridos 10 segundos. En el ejemplo se cierra el proceso si todavía se está ejecutando después de 10 segundos.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace ProcessSample
{
    class MyProcessClass
    {
        public static void Main()
        {
            try
            {
                using (Process 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}");
                            // 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) when (e is Win32Exception || e is FileNotFoundException)
            {
                Console.WriteLine("The following exception was raised: ");
                Console.WriteLine(e.Message);
            }
        }
    }
}
open System.ComponentModel
open System.Diagnostics
open System.IO
open System.Threading


try
    use myProcess = Process.Start "Notepad.exe"
    // Display physical memory usage 5 times at intervals of 2 seconds.
    let mutable i = 0

    while i < 5 && not myProcess.HasExited do
        // Discard cached information about the process.
        myProcess.Refresh()
        // Print working set to console.
        printfn $"Physical Memory Usage: {myProcess.WorkingSet64}"
        // Wait 2 seconds.
        Thread.Sleep 2000
        i <- i + 1
    // Close process by sending a close message to its main window.
    myProcess.CloseMainWindow() |> ignore
    // Free resources associated with process.
    myProcess.Close()
with
| :? Win32Exception
| :? FileNotFoundException as e ->
    printfn "The following exception was raised: "
    printfn $"{e.Message}"
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.IO
Imports System.Threading

Namespace Process_Sample
    Class MyProcessClass

        Public Shared Sub Main()
            Try
                Using myProcess = Process.Start("Notepad.exe")
                    ' Display physical memory usage 5 times at intervals of 2 seconds.
                    Dim i As Integer
                    For i = 0 To 4
                        If Not myProcess.HasExited Then

                            ' Discard cached information about the process.
                            myProcess.Refresh()
                            ' Print working set to console.
                            Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}")
                            ' Wait 2 seconds.
                            Thread.Sleep(2000)
                        Else
                            Exit For
                        End If

                    Next i

                    ' Close process by sending a close message to its main window.
                    myProcess.CloseMainWindow()
                    ' Free resources associated with process.
                    myProcess.Close()
                End Using
            Catch e As Exception When TypeOf e Is Win32Exception Or TypeOf e Is FileNotFoundException
                Console.WriteLine("The following exception was raised: ")
                Console.WriteLine(e.Message)
            End Try
        End Sub
    End Class
End Namespace 'Process_Sample

Comentarios

El Close método hace que el proceso deje de esperar a la salida si estaba esperando, cierra el identificador del proceso y borra las propiedades específicas del proceso. Close no cierra la salida estándar, la entrada y los lectores de errores y escritores en caso de que se haga referencia externamente.

Nota:

El Dispose método llama a Close. Al colocar el Process objeto en un using bloque, se eliminan los recursos sin necesidad de llamar Closea .

Se aplica a