00001 # Copyright (C) 2008-2009 Mark Hanegraaff 00002 # 00003 # This program is free software: you can redistribute it and/or modify 00004 # it under the terms of the GNU General Public License as published by 00005 # the Free Software Foundation, either version 3 of the License, or 00006 # (at your option) any later version. 00007 # 00008 # This program is distributed in the hope that it will be useful, 00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 # GNU General Public License for more details. 00012 # 00013 # You should have received a copy of the GNU General Public License 00014 # along with this program. If not, see <http://www.gnu.org/licenses/>. 00015 00016 00017 00018 from threading import Thread, Event 00019 00020 ## Base class for all thread based objects. 00021 # This class is basically a representation of a thread, which can be started, stopped and restarted on command. 00022 # appThreadBase derived objects are typically alive for the duration of most of the application. 00023 class appThreadBase(Thread): 00024 00025 ## Constructor method 00026 # @param _parent pointer. It could be anything, but usually whatever object starts the thread 00027 def __init__(self, _parent): 00028 Thread.__init__(self) 00029 self.parent = _parent 00030 00031 self.threadEvent = Event() 00032 self.threadEvent.set() 00033 00034 self.killSwitch = False; 00035 00036 ## Resets the members to their original state 00037 def reset(self): 00038 self.threadEvent.set() 00039 self.killSwitch = False; 00040 00041 ## Sets the killSwitch flag to True. The KillSwitch is just the flag which causes the while() loop 00042 # in the run method to end. 00043 def killSignal(self): 00044 self.killSwitch = True; 00045 00046 ## returns the value of the killSwitch flag 00047 # @return Boolean 00048 def getKillSignal(self): 00049 return self.killSwitch; 00050 00051 00052 ## Waits for threadEvent event to be set. 00053 # @param timeSecs Number of seconds to wait. 00054 def wait(self, timeSecs): 00055 self.threadEvent.wait(timeSecs) 00056 00057 00058 ## Just calls killSignal() and wiat() 00059 # @param timeSecs Number of seconds to wait. 00060 def killSignalAndWait(self, timeSecs): 00061 self.killSignal() 00062 self.wait(timeSecs) 00063 00064 00065
1.5.8