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);
WORD nextThreadNumber(0);
HWND dummyWindow;
#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
{
DLLEXPORT bool Start()

View File

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

View File

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