-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInvoke.cpp
91 lines (71 loc) · 1.89 KB
/
Invoke.cpp
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "stdafx.h"
#include "Invoke.h"
#include "Thread.h"
#include "MemoryUtil.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
namespace Invoke
{
struct Entry
{
Entry() = default;
Entry( int Index , Invoke::CallbackType Callback ) : m_Index( Index ) , m_Callback(Callback) {}
int m_Index = -1;
Invoke::CallbackType m_Callback;
};
namespace
{
HWND _main_wnd = NULL;
CThread _invoke_thread;
HANDLE _queue_semaphore = NULL;
SyncList< Invoke::Entry > _func_list;
}
unsigned int __stdcall InvokeThreadCaller(void*)
{
while ( !_invoke_thread.IsReqStatus( ThreadReq::TERMINATING ) )
{
DWORD WaitRet = WaitForSingleObjectEx( _queue_semaphore , INFINITE , TRUE );
if ( WaitRet != WAIT_OBJECT_0 ) return -1;
Invoke::Entry Item;
if( !_func_list.PopFront( Item ) ) continue;
Invoke::Repeat Ret = (Invoke::Repeat)SendMessage( _main_wnd , WM_INVOKE_CALL_TIMER , (WPARAM)&Item.m_Callback , 0 );
if ( Invoke::Again == Ret )
{
_func_list.PushBack( Item );
ReleaseSemaphore( _queue_semaphore , 1 , NULL );
}
}
return 0;
}
BOOL Init( HWND h )
{
_queue_semaphore = CreateSemaphore( NULL , 0 , LONG_MAX , NULL );
if ( !_queue_semaphore ) return FALSE;
_main_wnd = h;
return _invoke_thread.Start( InvokeThreadCaller , NULL , FALSE );
}
BOOL Delete( int Index )
{
if ( _func_list.Count() <= 0 ) return FALSE;
int Item = _func_list.Delete( [Index]( const Invoke::Entry& Item ) { return Item.m_Index == Index; });
return ( Item > 0 );
}
void Release()
{
CLOSE_HANDLE( _queue_semaphore );
_main_wnd = NULL;
_func_list.Clear();
_invoke_thread.Exit( 0 );
}
int Add( Invoke::CallbackType Func )
{
if ( !_main_wnd ) return FALSE;
int Index = _func_list.Count();
_func_list.PushBack( Entry( Index , Func ) );
ReleaseSemaphore( _queue_semaphore , 1 , NULL );
return Index;
}
};