您的位置:首页 > 其它

使用timer控件创建一个简单的报警程序

2008-04-24 15:46 816 查看
简介:当我使用计算机工作时,我总是如此的专心致志,以至于每当我过了“一会儿”去看时间时,发现已经过了三个小时,而我却完全没有意识到!所以我决定使用我从CodeProject学来的C#技术,来创建一个简单的应用程序—使用timer对象来倒计时一个由我自己设定的时间,并一直循环播放一段wave音乐,直到你重设timer控件。timer对象基础首先你要知道的是,使用timer对象你需要访问如下命名空间:
usingSystem.Threading;
usingSystem.timers;
接下来,介绍一下创建一个timer的要点以及为这个timer对象的Elapsed事件设定事件委派。先创建一个timer对象,这里我定义我使用的timer为
timerClock
。接下来设定
Elapsed
事件委派,当事件被触发时,指定的委派将被调用,这里我定义我使用的委派名称为
Ontimer()
。接着,设定
Interval
属性,使用毫秒数值指示希望
Elapsed
事件被调用的间隔,这意味着,当我定义
Interval
属性为1000毫秒时,我定义的委派
Ontimer()
将每隔1000毫秒被调用一次,或者说是每隔1秒。最后,需要设定
Enabled
属性为true,以使这个timer对象开始工作。接下来,剩下的只是一个小问题—创建一个委派,在这个timer对象的
Elapsed
属性被触发时调用。如果你以前没有使用过委派,不用担心,它们很容易使用,只需要创建一个方法,用来接收适合你捕获事件的一些变量。针对
Elapsed
事件,这个委派需要接收一个普通对象和一个
ElapsedEventArgs
对象。
privateSystem.timers.timertimerClock=newSystem.timers.timer();
timerClock.Elapsed+=newElapsedEventHandler(Ontimer);
timerClock.Interval=1000;
timerClock.Enabled=true;
publicvoidOntimer(Objectsource,ElapsedEventArgse)
{
//Yourcodehere
}
报警程序中使用timer控件好的,介绍了这些基础,现在,我们来看在实际应用中的代码。注意,这里并不包括播放wave音乐和显示最小化图标的代码,完整的代码你可以在那个demo项目中看到,基本上我是直接从jowBlow撰写的《Lowlevelaudioplayers》中粘贴的播放wave的代码。在下面的代码中,你可以看到,我将实例化timer对象的方法放在我自己的初始化方法
Initializetimer()
中,这个方法将被类构造调用。并且我创建了两个方法,
inputToSeconds()
secondsToTime()
用来将字符串格式的时间格式转换为正型,以及一个反处理过程。这些方法只是用来帮助我们在TextBox控件中显示日期格式,这在整个应用的结构中,并不十分重要。其他的那些代码,是标准的VisualStudio.NET为WinForm程序生成的样板文件。
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Threading;
usingSystem.timers;
usingSystem.IO;
usingSystem.Reflection;
namespacetimerAlarm
{
publicclasstimerForm:System.Windows.Forms.Form
{
//ControlsandComponents
privateSystem.Windows.Forms.TextBoxtimerInput;
privateSystem.Windows.Forms.ButtonStartButton;
privateSystem.Windows.Forms.ButtonResetButton;
privateSystem.ComponentModel.IContainercomponents;
//timerandassociatedvariables
privateSystem.timers.timertimerClock=newSystem.timers.timer();
privateintclockTime=0;
privateintalarmTime=0;
publictimerForm()
{
InitializeComponent();
Initializetimer();
}
protectedoverridevoidDispose(booldisposing)
{
if(disposing)
{
if(components!=null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#regionWindowsFormDesignergeneratedcode
#endregion
publicvoidInitializetimer()
{
this.timerClock.Elapsed+=newElapsedEventHandler(Ontimer);
this.timerClock.Interval=1000;
this.timerClock.Enabled=true;
}
[STAThread]
staticvoidMain()
{
Application.Run(newtimerForm());
}
privatevoidtimerForm_Resized(objectsender,System.EventArgse)
{
if(this.WindowState==FormWindowState.Minimized)
{
this.Hide();
}
}
privatevoidStartButton_Click(objectsender,System.EventArgse)
{
this.clockTime=0;
inputToSeconds(this.timerInput.Text);
}
privatevoidResetButton_Click(objectsender,System.EventArgse)
{
try
{
this.clockTime=0;
this.alarmTime=0;
this.timerInput.Text="00:00:00";
}
catch(Exceptionex)
{
MessageBox.Show("ResetButton_Click():"+ex.Message);
}
}
publicvoidOntimer(Objectsource,ElapsedEventArgse)
{
try
{
this.clockTime++;
intcountdown=this.alarmTime-this.clockTime;
if(this.alarmTime!=0)
{
this.timerInput.Text=secondsToTime(countdown);
}
//SoundAlarm
if(this.clockTime==this.alarmTime)
{
MessageBox.Show("PlaySound");
}
}
catch(Exceptionex)
{
MessageBox.Show("Ontimer():"+ex.Message);
}
}
privatevoidinputToSeconds(stringtimerInput)
{
try
{
string[]timeArray=newstring[3];
intminutes=0;
inthours=0;
intseconds=0;
intoccurence=0;
int
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: