Friday, September 30, 2011

Friday 9.30.11

Imports System
Imports System.Linq
Imports System.Diagnostics

Namespace Apress.VisualBasicRecipes.Chapter06
Public Class Recipe06_07

Public Shared Sub Main()
'build the query to return the average and total
'physical memory used by all of the processes
'running on the current machine. The data is returned
'as an anonymous type that contains the aggregate data
Dim aggregateData = Aggregate proc In Process.GetProcesses _
Into AverageMemory = Average(proc.WorkingSet64), _
TotalMemory = Sum(proc.WorkingSet64)

'display the formatted results on the console
Console.WriteLine("Average Allocated Physical Memory: {0,6} MB", (aggregateData.AverageMemory / (1024 * 1024)).ToString("#.00"))
Console.WriteLine("Total Allocated Physical Memory: {0,6} MB", (aggregateData.TotalMemory / (1024 * 1024)).ToString("#.00"))

End Sub
End Class
End Namespace


Imports System
Imports System.Linq
Imports System.Diagnostics

Namespace Apress.VisualBasicRecipes.Chapter06
Public Class Recipe06_08

Public Shared Sub Main()

'build the query to return information for all processes running on the current machine
'the process.threads collection for each process will be counted using the Count method
'the data will be returned as anonymous types containing the name of the process
'and the number of threads
Dim query = From proc In Process.GetProcesses _
Order By proc.ProcessName _
Aggregate thread As ProcessThread In proc.Threads _
Into ThreadCount = Count(thread.Id) _
Select proc.ProcessName, ThreadCount

'run the query generated earlier and iterate through the results
For Each proc In query
Console.WriteLine("The {0} process has {1} threads.", proc.ProcessName, proc.ThreadCount.ToString)
Next
End Sub
End Class
End Namespace


Imports System
Imports System.Linq
Imports System.Diagnostics

Namespace Apress.VisualBasicRecipes.Chapter06
Public Class Recipe06_09

Public Shared Sub Main()
'build the query to return the minimum and maximum
'physical memory allocated by all of the processes
'running on the current machine
'the data is returned as an anonymous type that contain the aggregate data.
Dim aggregateData = Aggregate proc In Process.GetProcesses _
Into MinMemory = Min(proc.WorkingSet64), _
MaxMemory = Max(proc.WorkingSet64)
'display the formatted results on the consol
Console.WriteLine("Minimum Allocated Physical Memory: {0,6} MB", (aggregateData.MinMemory / (1024 * 1024)).ToString("#.00"))
Console.WriteLine("Maximum Allocated Physical Memory: {0, 6} MB", (aggregateData.MaxMemory / (1024 * 1024)).ToString("#.00"))


End Sub
End Class
End Namespace


Namespace Apress.VisualBasicRecipes.Chapter06
Public Class Recipe06_10
Public Shared Sub Main()

'build the query to return information for all processes
'running on the current machine and group them based on the mathematical floor of the allocated physical
'memory. The count, maximum, and minimum values for each group are calculated and returned as properties of the anonymous type.
'data is returned only for groups that have more than one process

Dim query = From proc In Process.GetProcesses _
Order By proc.ProcessName _
Group By MemGroup = Math.Floor((proc.WorkingSet64 / (1024 * 1024))) _
Into Count = Count(), Max = Max(proc.WorkingSet64), Min = Min(proc.WorkingSet64) _
Where Count > 1 Order By MemGroup

'run the query generated earlier and iterate through the results
For Each result In query
Console.WriteLine("Physical Allocated Memory Group: {0} MB", result.MemGroup)
Console.WriteLine("Minimum amount of physical memory" &
" allocated: {0} ({1})", result.Min, (result.Min / (1024 * 1024)).ToString("#.00"))
Console.WriteLine("Maximum amount of physical memory" &
" allocated: {0} ({1})", result.Max, (result.Max / (1024 * 1024)).ToString("#.00"))
Console.WriteLine()
Next


End Sub

End Class
End Namespace


Public Class Recipe06_11

Public Shared Sub Main()
'store a list of processes that will be monitored or that information
'should be gathered for
Dim processesToMonitor = New String() {"explorer", "iexplore", "lsass", "rundll32", "services", "winlogon", "svchost"}

'build the query to return informaton for all of the processes that should be monitored by joining them to
'the list of all processes running on the current computer.
'The count, maximum, and minimum values for each group are calculated and returned as properties of the anonymous type
'data is returned only for groups that have more than one process
Dim query = From proc In Process.GetProcesses _
Order By proc.ProcessName _
Join myProc In processesToMonitor _
On proc.ProcessName Equals myProc _
Select Name = proc.ProcessName, proc.Id, PhysicalMemory = proc.WorkingSet64

'run the query generated earlier and iterate through the results
For Each proc In query
Console.WriteLine("{0, -10} ({1,5}) - Allocated Physical " & _
" Memory: {2,5} MB", proc.Name, proc.Id, (proc.PhysicalMemory / (1024 * 1024)).ToString("#.00"))
Next
End Sub
End Class

No comments: