SOL9 Sample: MemoryMonitor

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * MemoryMonitor.cpp 
 * Copyright (c) 2015 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>
#include "resource.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);
 
    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&MemoryMonitor::exit, 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)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  try {
    Application applet(name, argc, argv);
    
    Args args;

    MemoryMonitor monitor(applet, name, args);
    monitor.realize();
    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}


Last modified: 1 Feb 2017

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