Process.Close 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
釋放與這個元件相關的所有資源。
public:
void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()
範例
下列範例會啟動記事本的實例。 然後,它會以 2 秒間隔擷取相關聯進程的實體記憶體使用量,最多 10 秒。 此範例會偵測進程是否在10秒之前結束。 如果程式仍在 10 秒後執行,則此範例會關閉進程。
#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
備註
如果進程正在等候、關閉進程句柄,並清除進程特定屬性,方法 Close 會導致進程停止等候結束。 Close 如果外部參考標準輸出、輸入和錯誤讀取器和寫入器,則不會關閉它們。