-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchdir.cpp
More file actions
74 lines (65 loc) · 1.81 KB
/
watchdir.cpp
File metadata and controls
74 lines (65 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
71
72
73
74
#include <process.h>
#include "watchdir.hpp"
#include "guid.hpp"
#include "mix.hpp"
DirChangeNotifier::DirChangeNotifier(const wchar_t *path, HANDLE hPanel) :
m_path(path), m_hPanel(hPanel),
m_hChangeDir(INVALID_HANDLE_VALUE), m_fTerminate(false), m_fModified(0)
{
m_hTask = (HANDLE)_beginthreadex(
nullptr,
0x10000,
route,
this,
0,
nullptr);
}
DirChangeNotifier::~DirChangeNotifier() {
m_fTerminate = true;
if (m_hChangeDir != INVALID_HANDLE_VALUE)
FindCloseChangeNotification(m_hChangeDir);
if (m_hTask != 0) {
WaitForSingleObject(m_hTask, INFINITE);
CloseHandle(m_hTask);
}
}
unsigned int WINAPI DirChangeNotifier::route(void* arg) {
return reinterpret_cast<DirChangeNotifier*>(arg)->task();
}
unsigned int WINAPI DirChangeNotifier::task() {
m_hChangeDir = FindFirstChangeNotification(
m_path, // directory to watch
FALSE, // do not watch subtree
FILE_NOTIFY_CHANGE_FILE_NAME); // watch for file names
if (m_hChangeDir == INVALID_HANDLE_VALUE) {
OutputDebugStringW(L"ERROR: FindFirstChangeNotification");
_endthreadex(0);
return 0;
}
while (TRUE) {
DWORD dwWaitStatus = WaitForSingleObject(m_hChangeDir, INFINITE);
if (dwWaitStatus == WAIT_OBJECT_0) {
if (m_fTerminate) {
break;
}
// A file was created, renamed, or deleted in the directory.
// Refresh this directory and restart the notification.
if (isChecked()) {
::Info.AdvControl(&MainGuid, ACTL_SYNCHRO, 0, (void*)m_hPanel);
}
if (!FindNextChangeNotification(m_hChangeDir)) {
OutputDebugStringW(L"ERROR: FindNextChangeNotification");
break;
}
}
else {
OutputDebugStringW(L"WaitForSingleObject");
break;
}
}
if (!m_fTerminate)
FindCloseChangeNotification(m_hChangeDir);
m_hChangeDir = INVALID_HANDLE_VALUE;
_endthreadex(0);
return 0;
}