Re: Global functions?
Posted: Thu Sep 21, 2017 11:13 am
you cannot start it more then one time. you will have to create a new instance if you want to run it again.
here is a code block of something you can start and stop as you like. but you can still only have one running copy of it at a time.
here is a code block of something you can start and stop as you like. but you can still only have one running copy of it at a time.
Code: Select all
import threading
class ThreadClass(object):
def __init__(self):
self._event = threading.Event()
self._thread = None
def start(self, param1, param2):
if self._thread is None:
self._thread = threading.Thread(
name='SomeThreadName',
target=self.run,
args=(param1, param2)
)
self._thread.start()
def run(self, param1, param2):
eg.Bind('Main.OnClose', self.stop)
# startup code
print 'Thread started'
print str(param1)
print str(param2)
x = 1
# looping code
while not self._event.isSet():
print x
x += 1
if x == param2:
self._event.set()
else:
# non blocking sleep
self._event.wait(param1)
# closing code
eg.Unbind('Main.OnClose', self.stop)
print 'Thread closed'
def stop(self, dummyEvent=None):
if self._thread is not None:
self._event.set()
self._thread.join(1.0)
return False