Displaying memory status with VB.NET

 

When ScriptEZ.API based script file is running, an identification area is appended

to the system menu of the console windows as following:

 

 

Imports System

Imports System.Threading

 

'//////////////////////////////////////////////////////////////////////////////

' Author: SY

' Demo ScriptEZ.API based application which uses facilities from RAD API:

' . Memory Status Probing

' . Thread Pool usage

' . Echo() method usage which displays always messages into a console window

' . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe program

'   (Connect to ScriptEZ.API process with <processid (PID)> as connection name)

'

' Purpose: Displays memory usage info. from VB.NET/COM Interop

'

'//////////////////////////////////////////////////////////////////////////////

 

Module Module1

 Dim WithEvents obj As SCRIPTEZLib.API

 Dim exitProcess As Long

 Dim hThreadPool

 Dim oThread As Thread

 

 ' Main entry of the app

 Sub Main()

 

     exitProcess = 0

 

     ' Detach a new thread for instantiating ScriptEZ.API component

     oThread = New Thread(AddressOf WorkerThread)

 

     ' explicit thread apartment affinity to MTA (Multi Threaded Apartment)

     ' [ aka CoInitializeEx(NULL, COINIT_MULTITHREADED) in C/C++]

     'oThread.SetApartmentState(ApartmentState.MTA)

 

     ' start the thread

     oThread.Start()

 

     ' Endlessly loop until AppEventID = 3 sent from user with Remoting Control Interface

     Do Until exitProcess = 3

         Thread.Sleep(200)

     Loop

     oThread.Abort()

 

 End Sub

 

 ' .NET thread procedure

 Public Sub WorkerThread()

 

     ' instantiating the component

     obj = New SCRIPTEZLib.API

     obj.AppRegister()

 

     Dim r

     ' creating a thread pool object and start a job

     hThreadPool = obj.CreateTaskQueueObject(1)

     r = obj.AddTask(hThreadPool, 0)

 

     Console.ForegroundColor = ConsoleColor.Green

 

 End Sub

 

 ' Remoting Control Interface callback

 Private Function obj_AppEventHelp() As String Handles obj.AppEventHelp

     obj_AppEventHelp = CStr("Help" + vbLf + "Enter <3> to exit app")

 End Function

 

 ' Remoting Control Interface callback

 Private Sub obj_onAppEvent(ByVal EventID As Integer) Handles obj.onAppEvent

     exitProcess = EventID

 End Sub

 

 ' Thread pool callback

 Private Function obj_onRun(ByVal task_local_storage As Integer) As Integer Handles obj.onRun

 

   Thread.Sleep(1000) ' pause 1 second

 

   obj.ClearConsole()

   obj.SetConsoleTitle("Memory Observer: " + CStr(obj.ProbeMemoryStatus(0)) + "% of memory is in use")

   obj.Echo(vbLf + " Number of worker threads: " + CStr(obj.ProbeMemoryStatus(-4)) + vbLf)

   obj.Echo(vbLf + " " + CStr(obj.ProbeMemoryStatus(0)) + "% of memory is in use")

   obj.Echo(vbLf + " " + FormatNumber(obj.ProbeMemoryStatus(1), 0, 0, -2) + " Total Kbytes of physical memory")

   obj.Echo(vbLf + " " + FormatNumber(obj.ProbeMemoryStatus(2), 0, 0, -2) + " Free  Kbytes of physical memory")

   obj.Echo(vbLf + " " + FormatNumber(obj.ProbeMemoryStatus(3), 0, 0, -2) + " Total Kbytes of paging file")

   obj.Echo(vbLf + " " + FormatNumber(obj.ProbeMemoryStatus(4), 0, 0, -2) + " Free  Kbytes of paging file")

   obj.Echo(vbLf + " " + FormatNumber(obj.ProbeMemoryStatus(5), 0, 0, -2) + " Total Kbytes of virtual memory")

   obj.Echo(vbLf + " " + FormatNumber(obj.ProbeMemoryStatus(6), 0, 0, -2) + " Free  Kbytes of virtual memory" + vbLf)

   obj.Echo(vbLf + " " + FormatNumber(obj.ProbeMemoryStatus(7), 0, 0, -2) + " Kbytes in use by current process")

   obj_onRun = CLng(1) ' iterate this task again

 

 End Function

 

End Module