1 #include <Handler.h> 2 #include <Looper.h> 3 #include <Message.h> 4 5 #include <stdio.h> 6 7 const int32 kMessage = 'MeXX'; 8 9 class Looper : public BLooper { 10 public: 11 Looper(); 12 virtual void MessageReceived(BMessage *message); 13 }; 14 15 16 class Handler : public BHandler { 17 public: 18 Handler(); 19 virtual void MessageReceived(BMessage *message); 20 }; 21 22 int main() 23 { 24 Looper *looper = new Looper(); 25 Handler *handler = new Handler(); 26 looper->AddHandler(new Handler()); 27 looper->AddHandler(new Handler()); 28 looper->AddHandler(handler); 29 looper->AddHandler(new Handler()); 30 looper->AddHandler(new Handler()); 31 32 looper->Run(); 33 34 looper->PostMessage(new BMessage(kMessage), handler); 35 36 snooze(1000000); 37 38 looper->Lock(); 39 looper->Quit(); 40 } 41 42 43 Looper::Looper() 44 : 45 BLooper("looper") 46 { 47 } 48 49 50 void 51 Looper::MessageReceived(BMessage *message) 52 { 53 switch (message->what) { 54 case kMessage: 55 printf("Message received by Looper\n"); 56 break; 57 default: 58 BLooper::MessageReceived(message); 59 break; 60 } 61 } 62 63 64 Handler::Handler() 65 : 66 BHandler("handler") 67 { 68 } 69 70 71 void 72 Handler::MessageReceived(BMessage *message) 73 { 74 switch (message->what) { 75 default: 76 BHandler::MessageReceived(message); 77 break; 78 } 79 } 80