Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Task Manager for Windows Mobile and Windows CE

, 30 Nov 2008
Source code for writing your own Task Manager for Windows Mobile or Windows CE based devices
CETaskManager_demo.zip
Documents and Settings
Werner
My Documents
The CodeProject
CETaskManager_V2
CETaskManager.CAB
CETaskManager_src.zip
CETaskManager
CAB
TaskManager.vddproj
CEChart
bin
Release
CE
CEchart.dll
CEchart.pdb
CETaskManager
Properties
Settings.settings
Res
TaskMgr.ico
TaskMgr.ico
ToolBox
Idl
Res
CpuLoad.rgs
CpuLoadDCOM.rgs
Process.rgs
ProcessDCOM.rgs
ProcessList.rgs
ProcessListDCOM.rgs
System.rgs
SystemDCOM.rgs
ToolBox.def
ToolBox.rgs
ToolBoxdcom.rgs
Src
Threads
Tools
Windows
ToolBox.vcproj.ISAS5.Werner.user
ToolBoxTest
// CpuLoad.cpp : Implementation of CCpuLoad

#include "stdafx.h"
#include "CpuLoad.h"
#include "Tools\Exception.h"
#include <stdexcept>

CCpuLoad::CCpuLoad() :
    m_started(false),
    m_timerThread(1000, this),
    m_idleThread(),
    m_handle()
{
}

// CCpuLoad
HRESULT CCpuLoad::FinalConstruct()
{
	return S_OK;
}

void CCpuLoad::FinalRelease()
{
    m_timerThread.Stop();
    m_idleThread.Stop();
}

STDMETHODIMP CCpuLoad::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_ICpuLoad
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CCpuLoad::Start(void)
{
    try
    {
        Windows::AutoCriticalSection lock(m_lock);

        if (m_started) return S_FALSE;

        HANDLE handle = ::OpenEvent(EVENT_ALL_ACCESS , FALSE, L"CpuLoadEvent");
        if (handle != NULL)
        {
            Tools::Exception exception(CLSID_CpuLoad, IDS_ALREADY_STARTED_IN_OTHER_INSTANCE);
            return exception.GetException();
        }
        m_handle = ::CreateEvent(NULL, FALSE, FALSE, L"CpuLoadEvent");;

        m_idleThread.Start();
        m_timerThread.Start();

        m_started = true;
    } catch (std::exception& err)
    {
        const char* msg = err.what();
        Tools::Exception exception(CLSID_CpuLoad, IDS_FAILED_TO_START);
        return exception.GetException();
    }

    return S_OK;
}

STDMETHODIMP CCpuLoad::Stop(void)
{
    try
    {
        Windows::AutoCriticalSection lock(m_lock);

        if (!m_started) return S_FALSE;

        m_idleThread.Stop();
        m_timerThread.Stop();

        m_started = false;
    } catch (std::exception& err)
    {
        const char* msg = err.what();
        Tools::Exception exception(CLSID_CpuLoad, IDS_FAILED_TO_STOP);
        return exception.GetException();
    }

    return S_OK;
}

void CCpuLoad::OnTimer()
{
    static LARGE_INTEGER previousTime;
           LARGE_INTEGER currentTime;
           LARGE_INTEGER frequency;
    static LARGE_INTEGER previousThreadTime;
           LARGE_INTEGER currentThreadTime;
    LARGE_INTEGER currentKernelTime;
    LARGE_INTEGER currentUserTime;
    FILETIME creationTime;
    FILETIME exitTime;
    FILETIME kernelTime;
    FILETIME userTime;
    LARGE_INTEGER cpuLoadIdleThread;

    QueryPerformanceCounter(&currentTime);
    QueryPerformanceFrequency(&frequency);
    BOOL ret = GetThreadTimes(m_idleThread.GetThreadHandle(), &creationTime, &exitTime, &kernelTime, &userTime);

    // Convert all to LARGE_INTEGER
    currentKernelTime.HighPart = kernelTime.dwHighDateTime;
    currentKernelTime.LowPart  = kernelTime.dwLowDateTime;
    currentUserTime.HighPart   = userTime.dwHighDateTime;
    currentUserTime.LowPart    = userTime.dwLowDateTime;

    currentThreadTime.QuadPart = currentKernelTime.QuadPart + currentUserTime.QuadPart;
    cpuLoadIdleThread.QuadPart = ((currentThreadTime.QuadPart - previousThreadTime.QuadPart) / 10000 * 100) /
                                 ((currentTime.QuadPart - previousTime.QuadPart) * 1000 / frequency.QuadPart);

    previousTime = currentTime;
    previousThreadTime = currentThreadTime;

    // Fire COM event that will tell the CpuLoad of the last m_time
    Measurement(100 - cpuLoadIdleThread.LowPart/* % */);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Werner Willemsens
Team Leader
Belgium Belgium
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 30 Nov 2008
Article Copyright 2008 by Werner Willemsens
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid