Skip to content
Browse files

sample: Add an example for CModuleJob

Signed-off-by: Uli Schlachter <psychon@znc.in>
  • Loading branch information...
1 parent 308df21 commit 5a6224d2b4989f1cede45ba977c5b0a8c47f44fd @psychon psychon committed Aug 6, 2014
Showing with 31 additions and 0 deletions.
  1. +31 −0 modules/sample.cpp
View
31 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 @@ class CSampleMod : public CModule {
//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;
}

0 comments on commit 5a6224d

Please sign in to comment.
Something went wrong with that request. Please try again.