2013-08-01 115 views
0

下面的代碼應該是倒計時計時器:如何停止在Python線程(我有一個無限循環)

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() 

的「widget.executeAction(0)」行應該是定時器完成倒計時播放的聲音,但是我遇到的問題是聲音不停地播放。我如何殺死線程以便它只做一次?

+0

你能提供更完整的代碼嗎?這個代碼是如何調用的? – sdamashek

+0

線程在到達run()函數結束時應該自行退出。你確定這個錯誤不在你的線程調用中嗎?例如,你是不是很多次調用這個代碼? –

回答

1

「我遇到的問題是,聲音繼續播放不休」

這是definatly你的線程問題的重複呼叫。 (如果你提供的代碼調用上面發佈的代碼,我們肯定可以幫你找到它)。

在代碼中另外設置斷點或消息框並讓它告訴你(在線程啓動之前, )這種方式你知道你自己,它被稱爲多次...什麼時候。

希望這會有所幫助。

編輯:

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

thread =Thread(Timer()); 

thread.start() 

你開始你的線程中的線程? - 獲得了以上覆制代碼