From 5a6224d2b4989f1cede45ba977c5b0a8c47f44fd Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Wed, 6 Aug 2014 23:31:26 +0200 Subject: [PATCH] sample: Add an example for CModuleJob Signed-off-by: Uli Schlachter --- modules/sample.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/modules/sample.cpp b/modules/sample.cpp index c666b4b3..043bc8e5 100644 --- a/modules/sample.cpp +++ b/modules/sample.cpp @@ -20,6 +20,36 @@ using std::vector; +class CSampleJob : public CModuleJob { +public: + CSampleJob(CModule *pModule) : CModuleJob(pModule, "sample", "Message the user after a delay") {} + + ~CSampleJob() { + if (wasCancelled()) { + m_pModule->PutModule("Sample job cancelled"); + } else { + m_pModule->PutModule("Sample job destroyed"); + } + } + + virtual void runThread() { + // Cannot safely use m_pModule in here, because this runs in its + // own thread and such an access would require synchronisation + // between this thread and the main thread! + + for (int i = 0; i < 10; i++) { + // Regularly check if we were cancelled + if (wasCancelled()) + return; + sleep(1); + } + } + + virtual void runMain() { + m_pModule->PutModule("Sample job done"); + } +}; + class CSampleTimer : public CTimer { public: @@ -42,6 +72,7 @@ public: //AddTimer(new CSampleTimer(this, 300, 0, "Sample", "Sample timer for sample things.")); //AddTimer(new CSampleTimer(this, 5, 20, "Another", "Another sample timer.")); //AddTimer(new CSampleTimer(this, 25000, 5, "Third", "A third sample timer.")); + AddJob(new CSampleJob(this)); return true; }