LoginSignup
1

More than 5 years have passed since last update.

PIDからインスタンス名を取得する

Last updated at Posted at 2015-06-23

はじめ

CPU負荷が一定以上のアプリケーションを自動的にKillするアプリを作っていたのですが、どうもうまくタスクが消えていないという話しなので、調査したところ「プロセス名」と「インスタンス名」が違うという事が解りました

プロセス名とインスタンス名

プロセス名 = .exeを取り除いた物
 複数プロセスある場合でも同一名称、PIDは別になる。
 例:hoge.exe → hoge

インスタンス名
 通常はプロセス名と同一だが、複数立ち上げると後ろに番号がついていく。
 例:hoge hoge#1 hoge#2 hoge#3 ...等

コード

という事で、先人の知恵を。
http://kkamegawa.hatenablog.jp/entry/20060531/p1

VB.NETで書き換えます。

Module1.vb
     ' プロセスID→インスタンス名変換
    Function LookupInstanceName(pid) As String
        LookupInstanceName = ""
        Dim category = New PerformanceCounterCategory("Process")
        Dim instances = category.GetInstanceNames
        For Each instance In instances
            Dim c = New PerformanceCounter("Process", "ID Process", instance)
            If (pid = c.RawValue) Then
                LookupInstanceName = instance.ToString
                Return LookupInstanceName
            End If
        Next
        Return LookupInstanceName
    End Function

ぶつぶつ

繰り返し使うのであれば、配列を返すような関数としておいたほうがコード的には良いかと思います。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1