TIL timers go to the main thread

This commit is contained in:
Akash Mozumdar 2018-08-21 12:41:19 -04:00
parent 0b3599cb84
commit 19d7cdb180
3 changed files with 16 additions and 27 deletions

View File

@ -22,25 +22,9 @@ ThreadEventCallback onCreate(nullptr), onRemove(nullptr);
ProcessEventCallback onAttach(nullptr), onDetach(nullptr); ProcessEventCallback onAttach(nullptr), onDetach(nullptr);
WORD nextThreadNumber(0); WORD nextThreadNumber(0);
HWND dummyWindow;
#define HOST_LOCK CriticalSectionLocker hostLocker(&hostCs) // Synchronized scope for accessing private data #define HOST_LOCK CriticalSectionLocker hostLocker(&hostCs) // Synchronized scope for accessing private data
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID unused)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hinstDLL);
// jichi 8/24/2013: Create hidden window so that ITH can access timer and events
dummyWindow = CreateWindowW(L"Button", L"InternalWindow", 0, 0, 0, 0, 0, 0, 0, hinstDLL, 0);
break;
default:
break;
}
return true;
}
namespace Host namespace Host
{ {
DLLEXPORT bool Start() DLLEXPORT bool Start()

View File

@ -9,25 +9,32 @@
#include "../vnrhook/include/const.h" #include "../vnrhook/include/const.h"
#include "winmutex.h" #include "winmutex.h"
extern HWND dummyWindow;
#define TT_LOCK CriticalSectionLocker ttLocker(&ttCs) // Synchronized scope for accessing private data #define TT_LOCK CriticalSectionLocker ttLocker(&ttCs) // Synchronized scope for accessing private data
TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, DWORD status) : TextThread::TextThread(ThreadParameter tp, unsigned int threadNumber, DWORD status) :
storage(), storage(),
sentenceBuffer(), sentenceBuffer(),
status(status), status(status),
timestamp(GetTickCount()),
threadNumber(threadNumber), threadNumber(threadNumber),
output(nullptr), output(nullptr),
tp(tp) tp(tp)
{ {
InitializeCriticalSection(&ttCs); InitializeCriticalSection(&ttCs);
flushThread = CreateThread(nullptr, 0, [](void* textThread)
{
while (true)
{
Sleep(100);
((TextThread*)textThread)->FlushSentenceBuffer();
}
return (DWORD)0;
}, this, 0, nullptr);
} }
TextThread::~TextThread() TextThread::~TextThread()
{ {
EnterCriticalSection(&ttCs); EnterCriticalSection(&ttCs);
KillTimer(dummyWindow, (UINT_PTR)this);
LeaveCriticalSection(&ttCs); LeaveCriticalSection(&ttCs);
DeleteCriticalSection(&ttCs); DeleteCriticalSection(&ttCs);
} }
@ -45,9 +52,10 @@ std::wstring TextThread::GetStore()
return storage; return storage;
} }
void TextThread::AddSentence() void TextThread::FlushSentenceBuffer()
{ {
TT_LOCK; TT_LOCK;
if (timestamp - GetTickCount() < 250 || sentenceBuffer.size() == 0) return; // TODO: let user change delay before sentence is flushed
std::wstring sentence; std::wstring sentence;
if (status & USING_UNICODE) if (status & USING_UNICODE)
{ {
@ -80,12 +88,7 @@ void TextThread::AddText(const BYTE *con, int len)
{ {
TT_LOCK; TT_LOCK;
sentenceBuffer.insert(sentenceBuffer.end(), con, con + len); sentenceBuffer.insert(sentenceBuffer.end(), con, con + len);
SetTimer(dummyWindow, (UINT_PTR)this, 250, // TODO: Let user change delay before sentenceBuffer is flushed timestamp = GetTickCount();
[](HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
KillTimer(hWnd, idEvent);
((TextThread*)idEvent)->AddSentence();
});
} }
// EOF // EOF

View File

@ -42,7 +42,7 @@ public:
void Clear(); void Clear();
void AddText(const BYTE *con, int len); void AddText(const BYTE *con, int len);
void AddSentence(); void FlushSentenceBuffer();
void AddSentence(std::wstring sentence); void AddSentence(std::wstring sentence);
private: private:
@ -52,7 +52,9 @@ private:
std::wstring storage; std::wstring storage;
ThreadParameter tp; ThreadParameter tp;
HANDLE flushThread;
unsigned int threadNumber; unsigned int threadNumber;
DWORD timestamp;
DWORD status; DWORD status;
}; };