-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleManager.cpp
More file actions
70 lines (59 loc) · 1.81 KB
/
ConsoleManager.cpp
File metadata and controls
70 lines (59 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "ConsoleManager.h"
#include "DeviceMonitor.h"
#include <fcntl.h>
#include <io.h>
#include <wchar.h>
ConsoleManager::ConsoleManager(DeviceMonitor* diskMonitor)
: m_consoleThread(NULL), m_diskMonitor(diskMonitor) {
}
ConsoleManager::~ConsoleManager() {
StopConsoleThread();
}
void ConsoleManager::InitConsole() {
// 设置控制台宽字符模式
_setmode(_fileno(stdout), _O_U16TEXT);
_setmode(_fileno(stdin), _O_U16TEXT);
}
void ConsoleManager::SetMainThreadId(DWORD id) {
m_mainThreadId = id;
}
DWORD ConsoleManager::GetMainThreadId() {
return m_mainThreadId;
}
DeviceMonitor* ConsoleManager::GetDiskMonitor() {
return m_diskMonitor;
}
void ConsoleManager::StartConsoleThread() {
m_consoleThread = CreateThread(NULL, 0, ConsoleInputThread, this, 0, NULL);
if (!m_consoleThread) {
wprintf(L"创建控制台线程失败,错误代码: %d\n", GetLastError());
}
}
void ConsoleManager::StopConsoleThread() {
if (m_consoleThread) {
WaitForSingleObject(m_consoleThread, INFINITE);
CloseHandle(m_consoleThread);
m_consoleThread = NULL;
}
}
// 控制台输入线程实现
DWORD WINAPI ConsoleInputThread(LPVOID lpParam) {
ConsoleManager* consoleManager = static_cast<ConsoleManager*>(lpParam);
if (!consoleManager) return 1;
DeviceMonitor* pThis = consoleManager->GetDiskMonitor();
if (!pThis) return 1;
wchar_t buf[10] = { 0 };
while (true) {
if (fgetws(buf, sizeof(buf) / sizeof(wchar_t), stdin)) {
if (buf[0] == L'q' || buf[0] == L'Q') {
wprintf(L"收到退出命令,正在停止所有监控...\n");
PostThreadMessage(consoleManager->GetMainThreadId(), WM_QUIT, 0, 0);
Sleep(100);
pThis->StopMonitor();
break;
}
}
Sleep(100);
}
return 0;
}