none
how get time process RRS feed

  • Question

  • hello

     how get time process  cpu  and byet i use code but not work 

        Dim cc As New PerformanceCounter("Processor", "% Processor Time", "_Total")


    Thursday, September 5, 2019 12:30 AM

All replies

  •     

    Hi,

    I'm not sure if I can understand what you want to do.
    If you want to show CPU usage at certain intervals, such as 1 second, please use Timer.

    (1) make Timer start 
        Timer1.Interval = 1000
        Timer1.Start()

    (2) handle Timer1.Tick and show CPU usage

    Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
        ' --- performance counter
    End Sub

    Regards,


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Thursday, September 5, 2019 1:25 AM
  •     

    Hi,

    I'm not sure if I can understand what you want to do.
    If you want to show CPU usage at certain intervals, such as 1 second, please use Timer.

    (1) make Timer start 
        Timer1.Interval = 1000
        Timer1.Start()

    (2) handle Timer1.Tick and show CPU usage

    Private Sub Timer1_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
        ' --- performance counter
    End Sub

    Regards,


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    I want to get this  cpu number for example 0..00  I want to get value in cpu
    Thursday, September 5, 2019 1:50 AM
  • Hi,

    I have no idea about knowing each process's resource.
    Total CPU usage at one time can be gotten like this.
    You don't have to use "Form_Load", or  no need to call "prc_Write_File_CPU" and "prc_Read_File_CPU", these are only for getting CPU name.
    Private TextFileName As String = "C:\test\cpu.txt"
    ' --- Form: Load
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	Call Me.prc_Write_File_CPU()
    	Call Me.prc_Read_File_CPU()
    End Sub
    ' ---[Start] ボタン
    Private Sub Btn_Start_Click(sender As Object, e As EventArgs) Handles btn_Start.Click
    	Me.Timer1.Interval = 1000
    	Me.Timer1.Start()
    End Sub
    ' --- Timer.Tick
    Private Sub Timer_Tick(ByVal Sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
    	' --- カテゴリ名・カウンタ名・インスタンス名を指定して PerformanceCounter のインスタンスを作成
    	Dim pc As New System.Diagnostics.PerformanceCounter("Processor", "% Processor Time", "_Total", True)
    	If (pc IsNot Nothing) Then
    		Dim usage As Single = pc.NextValue()
    		Threading.Thread.Sleep(1000)    ' -- 待ちを入れて、NextValue を再取得する必要あり
    		usage = pc.NextValue()
    		Me.lbl_CPU_Usage.Text = (usage / 100).ToString("0.00%")
    	End If
    End Sub
    ' ---① CPU 情報をファイルに出力(wmic CPU List /Format > 出力ファイル)
    Private Sub prc_Write_File_CPU()
    	' --- 同名ファイルが存在すれば削除しておく
    	If (System.IO.File.Exists(Me.TextFileName)) Then
    		System.IO.File.Delete(Me.TextFileName)
    	End If
    	' --- wmic CPU List /Format の結果を出力ファイルにリダイレクトする
    	Dim p As New System.Diagnostics.Process()
    	With p
    		.StartInfo.FileName = System.Environment.GetEnvironmentVariable("ComSpec")
    		' --- 出力を読み取れるようにする
    		p.StartInfo.UseShellExecute = False
    		p.StartInfo.RedirectStandardOutput = True
    		p.StartInfo.RedirectStandardInput = False
    		' --- ウィンドウを表示しないようにする
    		p.StartInfo.CreateNoWindow = True
    		' --- コマンドラインを指定("/c"は実行後閉じるために必要)cf. chr(34) はダブルクォート
    		p.StartInfo.Arguments = "/c " _
    			& Chr(34) & "wmic CPU List /Format > " & Chr(34) & Me.TextFileName & Chr(34)
    	End With
    	' ----
    	p.Start()       ' -- プロセスを開始
    	p.WaitForExit() ' -- プロセス終了まで待機
    	p.Close()       ' -- プロセスを閉じる
    End Sub
    ' ---② ファイルを読み込み、CPU 名をラベルに表示
    Private Sub prc_Read_File_CPU()
    	' ---● テキストファイルを1行ずつ読み込む
    	Dim Line As String
    	Dim pos As Integer
    	Dim TextFile As System.IO.StreamReader
    	TextFile = New System.IO.StreamReader(Me.TextFileName, System.Text.Encoding.Default)
    	' ---▼▼ テキストファイルを最後まで読み込む
    	While TextFile.Peek > -1
    		' --- 1行読み込み
    		Line = TextFile.ReadLine
    		' --- "Name=" で始まっている行を探す
    		pos = Line.IndexOf("Name=")
    		If (pos = -1) Or (pos <> 0) Then
    			Continue While    ' -- "Name=" の行ではないので、ループ先頭に戻る
    		End If
    		pos = Line.IndexOf("=")
    		' --- Name の取得
    		Me.lbl_CUP_Name.Text = Line.Substring(pos + 1, Line.Length - pos - 1)
    	End While   ' --▲▲ テキストファイル・1行読み込みループの終端
    	' --- テキストファイルを閉じる
    	TextFile.Close()
    End Sub

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Thursday, September 5, 2019 3:24 AM
  • Hi,

    try my code:

    Imports System.Threading
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim processes As Process() = Process.GetProcesses()
            Dim counters = New List(Of PerformanceCounter)()
    
            For Each process As Process In processes
                Dim counter = New PerformanceCounter("Process", "% Processor Time", process.ProcessName)
                counter.NextValue()
                counters.Add(counter)
            Next
    
            Dim i As Integer = 0
            Thread.Sleep(1000)
            For Each counter In counters
                ListBox1.Items.Add(processes(i).ProcessName & "  | CPU% " & (Math.Round(counter.NextValue(), 1)))
                i += 1
            Next
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, September 5, 2019 5:44 AM
    Moderator