Click here to Skip to main content
15,893,722 members
Articles / Mobile Apps / Windows Mobile

Task Manager for Windows Mobile and Windows CE

Rate me:
Please Sign up or sign in to vote.
4.51/5 (23 votes)
30 Nov 2008CPOL10 min read 112.7K   7.3K   79  
Source code for writing your own Task Manager for Windows Mobile or Windows CE based devices
#include "stdafx.h"
#include <stdexcept>
#include "Threads\Thread.h"

namespace Threads
{

Thread::Thread() :
    m_stopped(true),
    m_thread()
{
}

Thread::~Thread()
{
}

void Thread::Start(unsigned int stackSize/* = 0x10000*/)
{
    Windows::AutoCriticalSection lock(m_lock);

    if (m_thread != INVALID_HANDLE_VALUE)
    {
        throw std::runtime_error("The thread is already started.");
    }

#ifdef _WIN32_WCE
    m_thread = ::CreateThread(0, stackSize, (LPTHREAD_START_ROUTINE)&RunEntry, this, CREATE_SUSPENDED, 0);
#else // _WIN32_WCE
    m_thread = (HANDLE)::_beginthreadex(0, stackSize, &RunEntry, this, 0, 0);
#endif // _WIN32_WCE

    if (m_thread == INVALID_HANDLE_VALUE)
    {
        unsigned error = GetLastError();
        throw std::runtime_error("Failed to start thread.");
    }

    // We can start
    m_stopped = false;

    ResumeThread(m_thread);
}

void Thread::Stop()
{
    m_stopped = true;
}

void Thread::Sleep(unsigned int msecs)
{
    ::Sleep(msecs);
}

void Thread::SetThreadPriority(int priority)
{
    Windows::AutoCriticalSection lock(m_lock);

    if (m_thread == INVALID_HANDLE_VALUE)
    {
        throw std::runtime_error("Thread is not running");
    }
    ::SetThreadPriority(m_thread, priority);
}

unsigned __stdcall Thread::RunEntry(void* arglist)
{
    Thread& thread = *(Thread*)arglist;
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    {
        thread.Run();
    }
    CoUninitialize();
    return 0;
}

} // namespace Threads

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)


Written By
Team Leader
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions