SOL9 2.0 Sample: MemoryMonitor

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * MemoryMonitor.cpp 
 * Copyright (c) 2009 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */



// SOL++2000

#include <sol\ApplicationView.h>
#include <sol\ScrolledText.h>
#include <sol\Profile.h>
#include <sol\MessageFont.h>
#include <sol\Stdio.h>
#include <sol\Thread.h>
#include <sol\LogFile.h>

namespace SOL {

class MemoryChecker :public Thread {
private:
    ScrolledText* sctext;
    Boolean looping;
    int        interval;
    LogFile logFile;
  
public:
    /**
     * Constructor
     */  
    MemoryChecker(ScrolledText* text, int mseconds)
        :logFile(_T(".\\memory.log")) {
        sctext = text;
        looping = true;
        this->interval = mseconds;
    }

public:
    ~MemoryChecker() {
        terminate();
    }

    void terminate() {
        looping = false;
        wait();
    }

public:
    void run() 
    {
        while (looping) {
            sleep(interval);
            // Get global memory.
            MEMORYSTATUS ms;
            memset(&ms, 0, sizeof(ms));
            ms.dwLength = sizeof(ms);
            GlobalMemoryStatus(&ms);
        
            double ftm = (double)ms.dwAvailPhys/1024.0; 
            double tm  = (double)ms.dwTotalPhys/1024.0; 

            sctext->printf(_T("Avail(KB)/Total(KB) = %.3f / %.3f\r\n"), 
                ftm, tm);
            logFile.printf(_T("Avail(KB)/Total(KB) = %.3f / %.3f\r\n"), 
                ftm, tm);
            
        }
    }
};


class MemoryMonitor :public ApplicationView {
private:
    Profile  profile;
    MessageFont     font;
    ScrolledText sctext;
    MemoryChecker memoryChecker;

public:
    /**
     * Constructor
     */
    MemoryMonitor(Application& applet, const TCHAR* name, Args& args)
    :ApplicationView(applet, name, args),
        memoryChecker(&sctext, 2000)
    {
        Args ar;
        font.create(9);

        ar.reset();
        sctext.create(this, NULL, ar);
        sctext.setReadOnly();

    // Add sctext to the default layout manager.
        add(sctext);

        sctext.limitText(32000); 
        sctext.setFont(font);
        sctext.addCallback(XmNmaxTextCallback, this, 
            (Callback)&MemoryMonitor::clear, null);

        addEventHandler(WM_CLOSE, this, 
            (Handler)&MemoryMonitor::close, NULL);

        // Start the memoryChecker thread.
        memoryChecker.start();

        restorePlacement();
    }

private:
    long MemoryMonitor::close(Event& event)
    {
        savePlacement();
        return defaultProc(event);
    }
private:
    void clear(Action& action)
    {
        sctext.setText(_T(""));
    }
};

}


// MemoryMonitor Main
void    Main(int argc, TCHAR** argv)
{
    const TCHAR* appClass = _T("MemoryMonitor - Physical Memory");
    try {
        Application applet(appClass, argc, argv);
        
        Args args;

        MemoryMonitor monitor(applet, appClass, args);
        monitor.realize();
        applet.run();
    } catch (...) {

    }
}


Last modified: 11 Nov 2009

Copyright (c) 2009 Antillia.com ALL RIGHTS RESERVED.