python允许无止境的循环码_如何停止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)“line应该是计时器倒数计时结束时播放的声音,但我遇到的问题是声音一直在播放。我如何杀死线程,使它只做一次?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值