2#include <event/SNotifyCenter.h>
3#include <helper/SFunctor.hpp>
16 UM_RUNONUISYNC = (WM_USER + 1000),
20 SNotifyReceiver(INotifyCallback *pCallback)
21 : m_pCallback(pCallback)
30 LRESULT OnRunOnUISync(UINT uMsg, WPARAM wParam, LPARAM lParam);
32 void OnTimer(UINT_PTR uID);
34 BEGIN_MSG_MAP_EX(SNotifyReceiver)
36 MESSAGE_HANDLER_EX(UM_RUNONUISYNC, OnRunOnUISync)
40 INotifyCallback *m_pCallback;
43LRESULT SNotifyReceiver::OnRunOnUISync(UINT uMsg, WPARAM wParam, LPARAM lParam)
45 IRunnable *pRunnable = (IRunnable *)lParam;
50void SNotifyReceiver::OnTimer(UINT_PTR uID)
52 if (uID == TIMERID_ASYNC)
55 m_pCallback->OnFireEvts();
60SNotifyCenter::SNotifyCenter(
int nInterval)
63 , m_nInterval(nInterval)
65 m_dwMainTrdID = GetCurrentThreadId();
66 m_pReceiver =
new SNotifyReceiver(
this);
67 m_pReceiver->CreateNative(_T(
"NotifyReceiver"), WS_POPUP, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, NULL);
68 SASSERT(m_pReceiver->IsWindow());
71SNotifyCenter::~SNotifyCenter(
void)
73 m_pReceiver->DestroyWindow();
77 SPOSITION pos = m_ayncEvent.GetTailPosition();
80 IEvtArgs *e = m_ayncEvent.GetNext(pos);
87 SASSERT(m_dwMainTrdID == GetCurrentThreadId());
98 m_pReceiver->SetTimer(SNotifyReceiver::TIMERID_ASYNC, m_nInterval, NULL);
101 m_ayncEvent.AddTail(e);
110 if (!e->IsBubbleUp())
113 SPOSITION pos = m_evtHandlerMap.GetTailPosition();
116 IEvtSlot *pSlot = m_evtHandlerMap.GetPrev(pos);
118 if (!e->IsBubbleUp())
125 SList<IEvtArgs *> evts;
126 SList<SAutoRefPtr<IRunnable>> cbs;
130 m_ayncEvent.RemoveAll();
131 cbs.Swap(m_asyncRunnable);
135 SPOSITION pos = evts.GetHeadPosition();
138 IEvtArgs *e = evts.GetNext(pos);
144 SPOSITION pos = cbs.GetHeadPosition();
155 for (SPOSITION pos = m_evtHandlerMap.GetHeadPosition(); pos;)
157 IEvtSlot *pSlot = m_evtHandlerMap.GetNext(pos);
158 if (pSlot->Equal(slot))
161 m_evtHandlerMap.AddTail(slot->Clone());
167 for (SPOSITION pos = m_evtHandlerMap.GetHeadPosition(); pos;)
169 SPOSITION posPrev = pos;
170 IEvtSlot *pSlot = m_evtHandlerMap.GetNext(pos);
171 if (pSlot->Equal(slot))
173 m_evtHandlerMap.RemoveAt(posPrev);
185 m_pReceiver->SendMessage(SNotifyReceiver::UM_RUNONUISYNC, 0, (LPARAM)pRunnable);
192 m_pReceiver->SetTimer(SNotifyReceiver::TIMERID_ASYNC, m_nInterval, NULL);
195 m_asyncRunnable.AddTail(pRunnable);
201 IRunnable *pRunable =
new StaticSFunctor2<FunRunOnUI, WPARAM, LPARAM>(fun, wp, lp);
209 IRunnable *pRunable =
new StdRunnable(fn);
216 IRunnable *pRunable =
new StdRunnable(fn);
Interface and Implementation for Native Window Handling.
Auto-lock class for managing critical sections.
Smart pointer class for managing COM-style reference-counted objects.
SEvent * GetEventObject(const DWORD dwEventID)
获取事件对象
void FireEvent(IEvtArgs *args)
触发事件
Class for managing native window operations.
BOOL KillTimer(UINT_PTR nIDEvent) OVERRIDE
Kills a timer for the window.
void FireEventAsync(IEvtArgs *e) OVERRIDE
触发一个异步通知事件
void RunOnUI2(FunRunOnUI fun, WPARAM wp, LPARAM lp, BOOL bSync) OVERRIDE
在UI线程中运行一个函数
BOOL RegisterEventMap(const IEvtSlot *slot) OVERRIDE
注册一个处理通知的对象
void FireEventSync(IEvtArgs *e) OVERRIDE
触发一个同步通知事件
void RunOnUIAsync(std::function< void(void)> fn)
在UI线程中异步运行一个闭包
virtual void OnFireEvts()
触发多个事件
void RunOnUI(IRunnable *pRunnable, BOOL bSync) OVERRIDE
在UI线程中运行一个可运行对象
void RunOnUISync(std::function< void(void)> fn)
在UI线程中同步运行一个闭包
virtual void OnFireEvent(IEvtArgs *e)
触发事件
BOOL UnregisterEventMap(const IEvtSlot *slot) OVERRIDE
注销一个处理通知的对象
static T * ms_Singleton
Static pointer to the singleton instance.