.Net Ought To Know #7 : What is a PID? How is it useful when troubleshooting a system?
If you have ever used the task manager (right-click on start bar and select Task Manager) and gone to the Process tab, then you have seen the processes that are running on your system. The default set-up of the task manager does not show the PID or Process ID. To add this, go to View/Add Columns on the menu and check the box next to PID(Process Identifer).
The PID is an integer that is assigned to each process in your operating system.It is usefull when you need to diagnose problems with your application since it allows you to uniquely identify each process.
In the .Net framework, you can access this information by using the System.Diagnostics namespace.
1 Imports System
2 Imports System.Diagnostics
3
4 Friend Class Class1
5 <STAThread()> _
6 Shared Sub Main(ByVal args As String())
7 Dim strRemark As String
8 ' remarks to insert into the console output
9
10 Console.WriteLine("all processes of the system")
11 Console.WriteLine()
12
13 Dim myProcesses As Process() = Process.GetProcesses()
14 ' all processes into the array
15
16 For Each p As Process In myProcesses
17 If p.Id = Process.GetCurrentProcess().Id Then
18 ' the process id is unique in the system
19 strRemark = " < = my application"
20 Else
21 strRemark = ""
22 End If
23
24 If p.ProcessName = _
25 Process.GetCurrentProcess().ProcessName _
26 AndAlso p.Id <> Process.GetCurrentProcess().Id Then
27 ' an additional instance of the same
28 ' application has the same name,
29 ' but an other process id
30
31 strRemark = " <= another instance of app"
32 End If
33
34 Console.WriteLine _
35 ("{0} {1} {2}", p.ProcessName, p.Id, strRemark)
36 Next p
37
38 Console.WriteLine()
39 Console.ReadLine()
40 ' this ReadLine command is to hold the application open
41 End Sub
42 End Class
Happy Programming.
Doc