自由之声论坛

首页 » 学习园地 » 数字家园 » 编程园地 » Process.Close 方法的VB.NET例子
山谷 - 2008-1-14 16:15:00
下面的示例启动一个记事本实例。然后它最多在 10 秒内,以两秒为间隔检索关联进程的物理内存使用情况。该示例检测该进程在经过 10 秒后是否退出。如果该进程在 10 秒后仍在运行,该示例就会将其关闭。
view plaincopy to clipboardprint?
Imports System 
Imports System.Diagnostics 
Imports System.Threading 
 
Namespace Process_Sample 
  Class MyProcessClass 
 
      Public Shared Sub Main() 
        Try 
 
            Dim myProcess As Process 
            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.ToString()) 
                  ' 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() 
 
        Catch e As Exception 
            Console.WriteLine("The following exception was raised: ") 
            Console.WriteLine(e.Message) 
        End Try 
      End Sub 'Main 
  End Class 'MyProcessClass 
End Namespace 'Process_Sample
1
查看完整版本: Process.Close 方法的VB.NET例子