user969334
user969334

Reputation: 53

How do I stop a Thread in Python (I have an endless loop)

The following code is supposed to be for a timer that counts down:

from org.csstudio.opibuilder.scriptUtil import PVUtil, ConsoleUtil, ColorFontUtil

from java.lang import Thread, Runnable

import threading

import time

startButton = display.getWidget("Start_Button")

stopButton = display.getWidget("Stop_Button")

resetButton = display.getWidget("Reset_Button")

bar = display.getWidget("Progress_Bar")

ECurrents = display.getWidget("Electron_Current")

PCurrents = display.getWidget("Positron_Current")

ELifetimes = display.getWidget("Electron Lifetimes")

PLifetimes = display.getWidget("Positron Lifetimes")

minText = display.getWidget("minText")

minPV=minText.getPV()

secText = display.getWidget("secText")

secPV=secText.getPV()

timerLabel = display.getWidget("timerLabel")

class Blink(Runnable):

    def run(self):

        i=0

        while PVUtil.getLong(pvs[2]) ==1:

            Thread.sleep(500)

            timerLabel.setPropertyValue("foreground_color", 

                        ColorFontUtil.YELLOW if i%2==0 else ColorFontUtil.RED)

            i=i+1

        timerLabel.setPropertyValue("foreground_color", ColorFontUtil.BLACK)

class Timer(Runnable):

    def run(self):        

        startButton.setEnabled(False)

        stopButton.setEnabled(True)

        resetButton.setEnabled(False)

        bar.setVisible(True)

        minText.setEnabled(False)

        secText.setEnabled(False)

        min = PVUtil.getLong(minPV)

        sec = PVUtil.getLong(secPV)

        #remember the values to be reset

        resetButton.setVar("min",120)

        resetButton.setVar("sec",0)

        timerLabel.setPropertyValue("foreground_color", ColorFontUtil.BLACK)

        timerLabel.setPropertyValue("text", "Time Left to Injection:")

        ECurrents.setPropertyValue("background_color", ColorFontUtil.GREEN)

        ELifetimes.setPropertyValue("background_color", ColorFontUtil.GREEN)

        PLifetimes.setPropertyValue("background_color", ColorFontUtil.GREEN)

        PCurrents.setPropertyValue("background_color", ColorFontUtil.GREEN)

        stopped=False

        total = (min*60)+(sec)

        for i in range(total,-1,-1):

            if not display.isActive():

                return

            if PVUtil.getLong(pvs[0])==0:

                stopped = True

                break            

            pvs[1].setValue(100-100*i/total)

            minPV.setValue(int(i/60))

            secPV.setValue(int(i%60))

            Thread.sleep(1000)            

        timerLabel.setPropertyValue("foreground_color", ColorFontUtil.RED)

        if (total==300):

            timerLabel.setPropertyValue("text", "5 minutes to injection!")

            PCurrents.setPropertyValue("background_color", ColorFontUtil.RED)

            ECurrents.setPropertyValue("background_color", ColorFontUtil.RED)

            ELifetimes.setPropertyValue("background_color", ColorFontUtil.RED)

            PLifetimes.setPropertyValue("background_color", ColorFontUtil.RED)

        if stopped:

            timerLabel.setPropertyValue("text", "Interrupted!")

        else:

            timerLabel.setPropertyValue("text", "Time's Up!!!")

            pvs[2].setValue(1)

            Thread(Blink()).start() 

        widget.executeAction(0)

        startButton.setEnabled(True)

        stopButton.setEnabled(False)

        resetButton.setEnabled(True)

        bar.setVisible(False)

        minText.setEnabled(True)

        secText.setEnabled(True)

if PVUtil.getLong(pvs[0])==1:

    thread =Thread(Timer());

    thread.start()

The "widget.executeAction(0)" line is supposed to be the sound that plays when the timer finishes counting down but the problem I'm having is that the sound keeps playing endlessly. How do I kill the thread so that it only does this once?

Upvotes: 0

Views: 220

Answers (1)

Don Thomas Boyle
Don Thomas Boyle

Reputation: 3045

"the problem I'm having is that the sound keeps playing endlessly"

This is definatly a repeating the call of your thread Issue. ( if you supply the code that calls the code you posted above im sure we can help you find it. )

Alternativly set breakpoints or Messageboxes in your code and have it tell you ( before the thread starts and while its in progress ) that way you know for youself that it is being called many times over... and when.

Hope this helps.

EDIT:

if PVUtil.getLong(pvs[0])==1:

thread =Thread(Timer());

thread.start()

You are starting the thread within your thread? - got this form your above copied code

Upvotes: 1

Related Questions