Global配置:Timer 计时器事件...

 Global配置:Timer 计时器事件...

分类: ASP.NET 配置 59人阅读 评论(0) 收藏 举报

 

[c-sharp] view plain copy print ?
  1. Global.asax:
  2. <%@ Application Language="C#" Inherits="路径.Global" CodeBehind="Global.cs"%>
  3. Global.cs:
  4. public class Global : System.Web.HttpApplication
  5. {
  6. public static Timer gtimer = null; // 定义全局定时器类对象
  7. protected void Application_Start(object sender, EventArgs e)
  8. {
  9. gtimer = new Timer(1000000); //16.7分钟执行一次方法
  10. // 将自定义用户函数(TimerEventFunction)指定为计时器的 Elapsed 事件处理程序
  11. // TimerEventFunction 可以写入自己的需要执行的代码逻辑
  12. gtimer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerEventFunction);
  13. // AutoReset 属性为 true 时,每隔指定时间间隔触发一次事件
  14. // 若赋值 false,则只执行一次
  15. gtimer.AutoReset = true;
  16. gtimer.Enabled = true;
  17. }
  18. protected void TimerEventFunction(Object sender, ElapsedEventArgs e)
  19. {
  20. //触发事件,相应的操作!
  21. }
  22. }

转载于:https://www.cnblogs.com/heartstill/archive/2012/06/05/2537473.html

import os import random import time from fnmatch import fnmatch import pygame import tkinter as tk from tkinter import * import wave import threading import tkinter import tkinter.filedialog import tkinter.messagebox import pyaudio root = tk.Tk() root.geometry("450x200+374+182") root.title("英语单词") english1 = "开始" w = Label(root, font=('times', 20, 'bold'), text=english1) w.pack() timer_running = False def word(): path = "D:\MY python\English" lists = os.listdir(path) english = (random.choice(lists)) global english1 english1 = english.strip(".wav") time.sleep(3) basedir = r"D:\MY python\English" for root, dirs, files in os.walk(basedir): for file in files: english3 = os.path.join(root, file) if fnmatch(file, f"{english1}*.wav"): pygame.mixer.init() pygame.mixer.music.load(english3) pygame.mixer.music.play() w.configure(text=f"{english1}") w.after(100, word) fileName = None allowRecording = False CHUNK_SIZE = 1024 CHANNELS = 2 FORMAT = pyaudio.paInt16 RATE = 44100 def record(): global fileName p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK_SIZE) wf = wave.open(fileName, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) while allowRecording: data = stream.read(CHUNK_SIZE) wf.writeframes(data) wf.close() stream.stop_stream() stream.close() p.terminate() fileName = None def start(): global allowRecording, fileName fileName = tkinter.filedialog.asksaveasfilename(filetypes=[('未压缩波形文件', '*.wav')]) if not fileName: return if not fileName.endswith('.wav'): fileName = fileName + '.wav' allowRecording = True start_timer() lbStatus['text'] = '正在录音中...' threading.Thread(target=record).start() def stop(): global allowRecording allowRecording = False lbStatus['text'] = '录音已结束' stop_timer() def closeWindow(): if allowRecording: tkinter.messagebox.showerror('Recording', 'Please stop recording before close the window.') return root.destroy() def tick(): global sec sec += 1 time['text'] = sec # Take advantage of the after method of the Label if timer_running: time.after(1000, tick) def start_timer(): global timer_running timer_running = True tick() def stop_timer(): global timer_running, sec timer_running = False sec = 0 time['text'] = sec button = tk.Button(text="开始", command=word) button.pack() btnStart = tkinter.Button(root, text='开始录音', command=start) btnStart.pack() btnStop = tkinter.Button(root, text='结束录音', command=stop) btnStop.pack() lbStatus = tkinter.Label(root, text='录音已准备', anchor='w', fg='green') lbStatus.pack() root.protocol('WM_DELETE_WINDOW', closeWindow) time = Label(root, fg='green') time.pack() root.mainloop()
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值