⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.cpp

📁 wince6.0平台上的任务管理器,功能类似于windows的任务管理器. In order to develop applications for Windows Mobile 6.x from
💻 CPP
字号:

#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -