Process.Close Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Libère toutes les ressources associées à ce composant.
public:
void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()
Exemples
L’exemple suivant démarre une instance du Bloc-notes. Il récupère ensuite l’utilisation de la mémoire physique du processus associé à intervalles de 2 secondes pendant un maximum de 10 secondes. L’exemple détecte si le processus se termine avant 10 secondes. L’exemple ferme le processus s’il est toujours en cours d’exécution après 10 secondes.
#using <System.dll>
using namespace System;
using namespace System::Diagnostics;
using namespace System::Threading;
int 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 : {0}", myProcess->WorkingSet.ToString() );
// Wait 2 seconds.
Thread::Sleep( 2000 );
}
else
{
break;
}
}
myProcess->CloseMainWindow();
// Free resources associated with process.
myProcess->Close();
}
catch ( Exception^ e )
{
Console::WriteLine( "The following exception was raised: " );
Console::WriteLine( e->Message );
}
}
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);
}
}
}
}
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
Remarques
La Close méthode fait en sorte que le processus cesse d’attendre la sortie s’il était en attente, ferme le handle de processus et efface les propriétés spécifiques au processus. Close ne ferme pas les lecteurs et les enregistreurs de sortie, d’entrée et d’erreur standard au cas où ils sont référencés en externe.
Notes
La méthode Dispose appelle Close. Placer l’objet Process dans un using
bloc supprime les ressources sans avoir à appeler Close.