xsicex

Forum

=> Noch nicht angemeldet?

Es gibt ein Problem mit dem BB-Code dieser betrifft jedoch nur den [code]-Teil. Darum bitte wir jegliche Codes vorübergehend mit [quote] zu gestalten.

Forum - Teil 6 von Bagplatt´s Tutorial Reihe

Du befindest dich hier:
Forum => Tutorial-Visual Basic => Teil 6 von Bagplatt´s Tutorial Reihe

<-Zurück

 1 

Weiter->


Bagplatt
(10 Posts bisher)
12.01.2011 21:44 (UTC)[zitieren]
Hallo zusammen,
Wir schreiben heute ein Programm dass alle Prozesso und dessen Thread´s ausliest.
Somit wäre es z.B. mit einem anderen Programm möglich Jeden Thread geziel zu löschen bzw. zu umgehen. Hier ersteinmal der Source Code.

Zitat:
Public Class Form1
Private ProcObj As Process = New Process()

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
listView1.Columns.Add("Base Name", 110, HorizontalAlignment.Left)
listView1.Columns.Add("Modules", 70, HorizontalAlignment.Right)
listView1.Columns.Add("Full Path", 150, HorizontalAlignment.Left)
listView1.Columns.Add("PID", 40, HorizontalAlignment.Right)
listView1.Columns.Add("CPU Time", 70, HorizontalAlignment.Right)
listView1.Columns.Add("Mem Usage", 80, HorizontalAlignment.Right)
listView2.Columns.Add("Thread ID", 85, HorizontalAlignment.Right)
listView2.Columns.Add("State", 100, HorizontalAlignment.Right)
listView2.Columns.Add("WaitReason", 258, HorizontalAlignment.Left)
listView2.Columns.Add("CPU Time", 70, HorizontalAlignment.Right)
Me.enumProcs()
End Sub

Public Sub enumProcs()
Dim lvItem As ListViewItem
Dim ts As System.TimeSpan
' alle laufenden Prozesse besorgen:
Dim allProcs As Process() = Process.GetProcesses()
For i As Integer = 0 To allProcs.Length - 1
' stat. Methode der Process-Klasse!
' alle Prozesse und ihre in ListView ausgeben:
lvItem = Me.listView1.Items.Add(allProcs(i).ProcessName)
' Prozesse, in denen mindestens ein Modul läuft:
Try
lvItem.SubItems.Add(allProcs(i).Modules.Count.ToString())
lvItem.SubItems.Add(allProcs(i).MainModule.FileName)
Catch
lvItem.SubItems.Add("0"
lvItem.SubItems.Add("0"
Finally
lvItem.SubItems.Add(allProcs(i).Id.ToString())
' TimeSpan-Objekt besorgen:
Try
ts = allProcs(i).TotalProcessorTime
' Zeit formatieren:
lvItem.SubItems.Add(String.Format("{0:00}", ts.TotalHours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds))
Catch
lvItem.SubItems.Add("Err"
End Try
lvItem.SubItems.Add((allProcs(i).WorkingSet64 / 1000).ToString() + "K"
label1.Text = "Prozesse: " + allProcs.Length.ToString()
End Try
Next
End Sub

Public Sub EnumThreads(ByVal ProcID As Integer)
Dim ts As System.TimeSpan
Dim lvItem As ListViewItem
' ListView-Inhalt löschen:
listView2.Items.Clear()
' Prozess referenzieren (mittels seiner ID):
Dim aProc As Process = Process.GetProcessById(ProcID)
Try
label2.Text = "Threads: " + aProc.Threads.Count.ToString()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
' Alle Threads des Prozesses durchlaufen:
For Each aThread As ProcessThread In aProc.Threads
lvItem = listView2.Items.Add(aThread.Id.ToString())
Select Case aThread.ThreadState
Case ThreadState.Initialized
lvItem.SubItems.Add("Initialized"
Case ThreadState.Ready
lvItem.SubItems.Add("Ready"
Case ThreadState.Running
lvItem.SubItems.Add("Running"
Case ThreadState.Standby
lvItem.SubItems.Add("Standby"
Case ThreadState.Terminated
lvItem.SubItems.Add("Terminated"
Case ThreadState.Transition
lvItem.SubItems.Add("In Transition"
Case ThreadState.Unknown
lvItem.SubItems.Add("Unkwown"
Case ThreadState.Wait
lvItem.SubItems.Add("Waiting"
End Select
' Falls Thread im Wartezustand ist, soll Info ausgegeben werden:
If aThread.ThreadState = ThreadState.Wait Then
lvItem.SubItems.Add(ListReason(aThread.WaitReason))
Else
lvItem.SubItems.Add("N/A"
End If
' TimeSpan-Objekt abholen:
ts = aThread.TotalProcessorTime
' Zeit formatieren:
lvItem.SubItems.Add(String.Format("{0:00}", ts.TotalHours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds))
Next
End Sub
' Diese Methode dechiffriert die ThreadwaitReason-Enumeration:
Private Function ListReason(ByVal waitingReason As System.Diagnostics.ThreadWaitReason) As String
Dim s As String = ""
Select Case waitingReason
Case ThreadWaitReason.EventPairHigh
s = "Waiting For Event Pair High"
Case ThreadWaitReason.EventPairLow
s = "Waiting For Event Pair Low"
Case ThreadWaitReason.ExecutionDelay
s = "Execution Delay"
Case ThreadWaitReason.Executive
s = "Waiting for Scheduler"
Case ThreadWaitReason.FreePage
s = "Waiting For Free Virtual Mem. Page"
Case ThreadWaitReason.LpcReceive
s = "Waiting For A Local Proc. Call To Arrive"
Case ThreadWaitReason.LpcReply
s = "Waiting For A Reply To A Local Proc. Call"
Case ThreadWaitReason.PageIn
s = "Waiting For Virtual Mem. Page To Arrive In Memory"
Case ThreadWaitReason.PageOut
s = "Waiting For Virtual Mem. Page To Write To Disk"
Case ThreadWaitReason.Suspended
s = "Execution Suspended"
Case ThreadWaitReason.SystemAllocation
s = "Waiting For A System Allocation"
Case ThreadWaitReason.Unknown
s = "Waiting For Unknown Reason"
Case ThreadWaitReason.UserRequest
s = "Waiting For A User Request"
Case ThreadWaitReason.VirtualMemory
s = "Waiting For Virtual Memory"
End Select
Return s
End Function




Private Sub listView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listView1.SelectedIndexChanged
Try
EnumThreads(Convert.ToInt32(listView1.SelectedItems(0).SubItems(3).Text))
Catch
End Try
End Sub

Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
Me.Close()
End Sub
End Class


Kopiert ihn euch in VB. Ich habe alle Defenintionen dabei geschrieben.

MFG Bagplatt

Antworten:

Dein Nickname:

 Schriftfarbe:

 Schriftgröße:
Tags schließen



Themen gesamt: 23
Posts gesamt: 29
Benutzer gesamt: 4
Derzeit Online (Registrierte Benutzer): Niemand crying smiley
 
Gesamt 11031 Besucher
Impressum: -Website: Sice -Moderatoren: Bagplatt -Design: Dj Rezo
Diese Webseite wurde kostenlos mit Homepage-Baukasten.de erstellt. Willst du auch eine eigene Webseite?
Gratis anmelden