VB. NET implements a super ptm Viewer

Source: Internet
Author: User
First, describe the article name. P represents process, that is, process, t represents thread, that is, thread, and m represents module, that is, module.

There is a performance viewer in Win NT, Win 2000, and Win XP. We can use it to view the program running on the computer and obtain the main window name related to the program, and view the usage of CPU, memory, and virtual pages. However, for computer users with higher requirements, these are not enough, especially some DIY enthusiasts who want to view their computers at any time, learn more to know whether your computer should be upgraded, whether to adjust the virtual page, whether to change the CPU or increase the memory.

In this case, we have a reason to make a better viewer. Before writing code, we also need to know some basic knowledge, namely proccess, thread, and module. Now let's take a look at the basic knowledge of proecss.

  1. Concepts of Proccss

In the simplest words, a process is the currently running application. The running applications include EXE and DLL files. The difference between files and processes is that files are stored on disks and are static. Computers do not call files, but the process is not static, therefore, every time a computer calls a process, a certain amount of resources are required. These resources include memory, virtual pages, handles, GDI ......

In. net, we can use the process class (which is located in the system. Diagnostics space) to provide access to processes running on the computer. You can use the process class to obtain the list of processes currently running or start a new process. The process class is used to access system processes. After the process class is initialized, you can use this class to obtain information about the currently running process. This information includes the thread set, loaded modules (. dll and. EXE files), and performance information (such as the amount of memory currently used by the process ).

A system process is uniquely identified by its process identifier. Like many Windows resources, a process is identified by its handle, which may not be unique on a computer. A handle is a general term used to indicate a resource identifier. Even if the Process has exited, the operating system still keeps the Process Handle, which is accessed through the Handle attribute of the Process component. Therefore, you can obtain the management information of processes, such as ExitCode (usually, or zero indicates success, or non-zero error code) and ExitTime. Handle is a very valuable resource.

  2. thread Concept

A process consists of one or more threads. In other words, a thread divides a process into several small parts. Each thread has a priority. When a thread with a higher priority completes a task, the thread with a lower priority may be forced to wait. On a multi-processor computer, multiple threads are moved to different processors to balance the CPU load. Each process has a thread at startup, which is called the main thread. Any thread can create other threads. All threads in the process share the address space of the process.

In. net can use System. the ProcessThread class of Diagnostics to obtain thread information related to the process. use System. threading Thread class to create a Thread. note that the Thread class cannot be used to access threads of other processes. It can only be used to create one Thread. Remember this. If you want to perform more operations on the thread, you can use the id returned by the System. Diagnostics. ProcessThread class to operate the thread as a parameter of the api function.

  3. module Concept

A module refers to an executable file or a dynamic link library (DLL ). Each process contains one or more modules. You can use System. the ProcessModule class of Diagnostics is used to obtain information about the module. This class has a very useful attribute, EntryPointAddress, which can return the memory address of the function that runs when the system loads and runs the module. Then, use the tool in Visual Studio6.0 to view the address to know what function it is! Then, you can view relevant information based on the function name to understand the basic programming idea of the application. This function is useful to programmers.

Now that you know the basic knowledge, you can program it. First, create a window and add a TabControl paging control to the window. Add label1, label2, listbox1, listbox2, and button1 to the first page. Add label4, label6, listbox3, and listbox4 on the second page, and add label5, label7, listbox5, and listbox6 on the third page. Set the HorizontalScrollbar attribute of all listbox controls to true.

Then add the following code (written in vb.net)

Public Class Form1
Inherits System. Windows. Forms. Form
Public zj
Public myProcesses () As Process
Public myprocess as process
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Listbox1.items. Clear ()
Myprocesses = process. getprocesses ()
For each myprocess in myprocesses
Listbox1.items. Add (myprocess. processname)
Next
Label1.text = ""
Label1.text = "local total" & listbox1.items. Count & "processes running"
End sub

Private sub listboxshortdoubleclick (byval sender as system. Object, byval e as system. eventargs) handles listbox1.doubleclick
Dim I: dim F as integer: dim h as integer
Listbox2.items. Clear ()
ZJ = listbox1.selectedindex
Label2.text = "process" & listbox1.items. Item (zj) & "related information"
With listbox2.items
If myprocesses (zj). mainwindowtitle = "" then
. Add ("no related main window ")
Else
. Add (myProcesses (zj). MainWindowTitle)
End If
. Add (myProcesses (zj). BasePriority & "Basic Process Priority ")
. Add (myProcesses (zj). Handle. ToString & "Native Handle of the Process ")
. Add (myProcesses (zj). HandleCount & "Number of handles opened by the process ")
. Add (myProcesses (zj). Id & "Unique Identifier of the Process ")
. Add (myProcesses (zj). MachineName & "Name of the computer on which the process is running ")
. Add (myProcesses (zj). MainModule. ToString & "Main module of the Process ")
. Add (myProcesses (zj). main1_whandle. ToString & "handle to the main process window ")
'. Add (myProcesses (zj). Modules &"")
. Add (myProcesses (zj). NonpagedSystemMemorySize & "unpaged system memory size allocated to this process ")
. Add (myProcesses (zj). PagedMemorySize & "Paging memory size ")
. Add (myProcesses (zj). PagedSystemMemorySize & "Paging System memory size ")
. Add (myprocesses (zj). peakpagedmemorysize & "peak paging memory size ")
. Add (myprocesses (zj). peakvirtualmemorysize & "peak virtual memory size ")
. Add (myprocesses (zj). privatememorysize & "dedicated memory size ")
. Add (myprocesses (zj). starttime & "process start time ")
. Add (myprocesses (zj). totalprocessortime. Seconds & "Total processor time of the Process ")
. Add (myprocesses (zj). userprocessortime. Seconds & "process user processor time ")
. Add (myprocesses (zj). virtualmemorysize & "virtual memory size of processes ")
End
Listbox3.items. Clear ()
Tabpage1.text = "process" & listbox1.items. Item (zj) & "basic information"
Tabpage2.text = "process" & listbox1.items. Item (zj) & "related thread information"
Tabpage3.text = "process" & listbox1.items. Item (zj) & "related module information"
'/////////////////////////////////////// //////
F = myprocesses (zj). threads. Count
Label4.text = "process" & listbox1.items. Item (zj) & "Total" & F & "related threads. The thread ID information is as follows :"
Dim int, PK, m, n
For n = 0 to F-1
Listbox3.items. Add (myprocesses (zj). threads (N). Id)
Next
'///////////////////////////////////////

Listbox5.items. Clear ()
Dim Zha, KL
Zha = myprocesses (zj). modules. Count
Label5.text = "process" & listbox1.items. Item (zj) & "Total" & Zha & "modules. The module location information is as follows :"
For KL = 0 to Zha-1
Listbox5.items. Add (myprocesses (zj). Modules (kL). filename)
Next
End sub

Private sub listbox3_doubleclick (byval sender as object, byval e as system. eventargs) handles listbox3.doubleclick
Listbox4.items. Clear ()
Dim F, n
F = myprocesses (zj). threads. Count
N = ListBox3.SelectedIndex
Label6.Text = "Thread" & ListBox3.Items. Item (n) & "related information"
For process-related thread information, see System. Diagnostics. processThread. Because the Processes. threads attribute returns the thread object array, and the instance of the thread object array is created by the ProcessThread class, the information of the thread object array is contained in the System. Diagnostics. ProcessThread class.
. Add (myProcesses (zj). Threads (n). BasePriority)
. Add (myProcesses (zj). Threads (n). CurrentPriority)
. Add (myProcesses (zj). Threads (n). PriorityLevel)
. Add (myProcesses (zj). Threads (n). PrivilegedProcessorTime)
. Add (myProcesses (zj). Threads (n). StartTime)
. Add (myProcesses (zj). Threads (n). ThreadState. ToString)
. Add (myProcesses (zj). Threads (n). TotalProcessorTime. Seconds)
. Add (myProcesses (zj). Threads (n). UserProcessorTime. Seconds)
. Add (myProcesses (zj). Threads (n). WaitReason. ToString)
End
End Sub

Private Sub ListBox5_DoubleClick (ByVal sender As Object, ByVal e As System. EventArgs) Handles ListBox5.DoubleClick
ListBox6.Items. Clear ()
Dim f, n, m
F = myProcesses (zj). Modules. Count
N = ListBox5.SelectedIndex
Label7.Text = "module" & ListBox5.Items. Item (n) & "related information"
With ListBox6.Items 'process-related module information can be found in System. diagnostics. processModule class, because Processes. the Modules attribute returns the Modules object array, while the instance of the Modules object array is created by the ProcessModule class. The information of the Modules object array is contained in System. diagnostics. processModule class.
M = Hex (myProcesses (zj). Modules (n). BaseAddress. ToInt32) 'to hexadecimal
. Add (m)
M = Hex (myProcesses (zj). Modules (n). EntryPointAddress. ToInt32)
. Add (m)
. Add (myProcesses (zj). Modules (n). FileVersionInfo)
. Add (myProcesses (zj). Modules (n). ModuleMemorySize)
. Add (myProcesses (zj). Modules (n). ModuleName)
End
End sub

End Class: this is a big success. At last, I would like to provide some additional explanations for the program. When you know how to write this program, you may think that it is meaningless to write a simple program. How can you make the program more meaningful? Let me tell you. In fact, this code is not written by me, but a part of remote control in a LAN. Scan the Processes running on the client, view the client resources, send the obtained information to a server, and close the Processes on the client through the Processes class, so that the manager can manage the client. In addition, another purpose of this article is to allow readers to quickly master the System. Diagnostics. Process, System. Diagnostics. processThread, and System. Diagnostics. ProcessModule classes in. net. Passed in vb.net and win2000.

Lead http://www.yesky.com/20020826/1627104.shtml

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.