thread.cpp

来自「wince6.0平台上的任务管理器,功能类似于windows的任务管理器. 」· C++ 代码 · 共 79 行

CPP
79
字号

#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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?