There are precautions about using threading with enigma and enigma experts advice to avoid using threading but if we follow some rules as experts said regarding using threading outside main thread we can use threading safely.
The following code demonstrates not real plugin to run simple counter thread just to simplify the problem.
although the counter runs well and can stop it at any moment but has problem in showing another screen if i want in opensource images and start enigma without error notification in dreamos enigma
what is the proper way to use the following simple thread?
from Screens.Screen import Screen from Screens.MessageBox import MessageBox from Components.ActionMap import NumberActionMap,ActionMap from Components.Pixmap import Pixmap from Components.Label import Label from Plugins.Plugin import PluginDescriptor import threading import time #### class Test(Screen): skin = """ <screen name="myscreen" position="center,center" size="1000,700" title="Test thread" > <widget name="text" position="250,100" size="300,100" font="Regular;22" /> <eLabel position = "250,370" zPosition = "4" size = "300,25" halign = "center" font = "Regular;24" transparent = "0" foregroundColor = "white" backgroundColor = "green" text = "start counter" /> <eLabel position = "250,470" zPosition = "4" size = "300,25" halign = "center" font = "Regular;24" transparent = "0" foregroundColor = "white" backgroundColor = "red" text = "stop counter" /> </screen>""" def __init__(self, session): Screen.__init__(self, session) self['text']=Label('Press green to start counter ') self["actions"] = ActionMap(['ColorActions',"WizardActions" ], { "back": self.close, "green": self.startthread, "red": self.stopcounter, "ok": self.close, }, -1) self.exitcounter=False def startthread(self): self.active_thread = threading.Thread(target=self.startcounter, args=()) self.active_thread.daemon = True self.active_thread.start() def startcounter(self): self['text'].setText('Counter started') i=0 while i<60: i=i+1 print str(i) time.sleep(1) self['text'].setText(str(i)) if self.exitcounter: self.exitcounter=False break self['text'].setText('Counter stopped') def stopcounter(self): self.exitcounter=True def main(session, **kwargs): session.open(Test) def menu(menuid, **kwargs): if menuid == 'mainmenu': return [(_('testPlugin'), main, 'testPlugin_mainmenu', 1)] return [] def Plugins(**kwargs): list = [] try: list.append(PluginDescriptor(icon='plugin.png', name='testPlugin', description='test enigma', where=PluginDescriptor.WHERE_MENU, fnc=menu)) except: list.append(PluginDescriptor(icon='plugin.png', name='testPlugin', description='test enigma', where=PluginDescriptor.WHERE_MENU, fnc=menu)) list.append(PluginDescriptor(icon='plugin.png', name='testPlugin', description='test enigma', where=PluginDescriptor.WHERE_PLUGINMENU, fnc=main)) return list