works:programmer:cpp:thread
Поток с классом на WinAPI С++
Свободно компилируется на WATCOM C++, имеет минимальный код для запуска, остановки. Можно спокойно использовать как шаблон для много поточных программ.
- FileMonitor.hpp
#pragma once #include "stdafx.hpp" class FileMonitor { HANDLE hThreadEvent; HANDLE hThread; DWORD ThreadWorker(); static DWORD WINAPI cbThreadWorker(LPVOID lpParam); public: FileMonitor(); ~FileMonitor(); void Start(); void Stop(); };
- FileMonitor.cpp
#include "FileMonitor.hpp" FileMonitor::FileMonitor() { // Thread event hThreadEvent = CreateEvent(NULL, TRUE, FALSE, NULL); if ( hThreadEvent == NULL ) { return; } } DWORD WINAPI FileMonitor::cbThreadWorker(LPVOID lpParam) { return reinterpret_cast<FileMonitor*>(lpParam)->ThreadWorker(); } DWORD FileMonitor::ThreadWorker() { while ( WaitForSingleObject(hThreadEvent, 0) != WAIT_OBJECT_0 ) { MessageBox(0, L"", L"Thread event", MB_ICONINFORMATION); Sleep(10000); } return ERROR_SUCCESS; } FileMonitor::~FileMonitor() { CloseHandle(hThreadEvent); free(home); } void FileMonitor::Start() { hThread = CreateThread(NULL, 0, cbThreadWorker, this, 0, NULL); } void FileMonitor::Stop() { SetEvent(hThreadEvent); WaitForSingleObject(hThread, INFINITE); }
works/programmer/cpp/thread.txt · Последнее изменение: 2018/12/15 17:18 — 127.0.0.1