王朝网络
分享
 
 
 

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

王朝other·作者佚名  2006-01-08
宽屏版  字体: |||超大  

简介:

当我使用计算机工作时,我总是如此的专心致志,以至于每当我过了“一会儿”去看时间时,发现已经过了三个小时,而我却完全没有意识到!所以我决定使用我从Code Project学来的C#技术,来创建一个简单的应用程序—使用Timer 对象来倒计时一个由我自己设定的时间,并一直循环播放一段wave音乐,直到你重设timer控件。

Timer对象基础

首先你要知道的是,使用Timer对象你需要访问如下命名空间:

using System.Threading;

using System.Timers;

接下来,介绍一下创建一个Timer的要点以及为这个timer对象的Elapsed事件设定事件委派。

先创建一个Timer对象,这里我定义我使用的timer为timerClock。接下来设定Elapsed事件委派,当事件被触发时,指定的委派将被调用,这里我定义我使用的委派名称为OnTimer()。

接着,设定Interval属性,使用毫秒数值指示希望Elapsed事件被调用的间隔,这意味着,当我定义Interval属性为1000毫秒时,我定义的委派OnTimer()将每隔1000毫秒被调用一次,或者说是每隔1秒。

最后,需要设定Enabled属性为true,以使这个timer对象开始工作。接下来,剩下的只是一个小问题—创建一个委派,在这个timer对象的Elapsed属性被触发时调用。如果你以前没有使用过委派,不用担心,它们很容易使用,只需要创建一个方法,用来接收适合你捕获事件的一些变量。

针对Elapsed事件,这个委派需要接收一个普通对象和一个ElapsedEventArgs对象。

private System.Timers.Timer timerClock = new System.Timers.Timer();

timerClock.Elapsed += new ElapsedEventHandler(OnTimer);

timerClock.Interval = 1000;

timerClock.Enabled = true;

public void OnTimer( Object source, ElapsedEventArgs e )

{

//Your code here

}

在报警程序中使用Timer控件

好的,介绍了这些基础,现在,我们来看在实际应用中的代码。注意,这里并不包括播放wave音乐和显示最小化图标的代码,完整的代码你可以在那个demo项目中看到,基本上我是直接从jow Blow撰写的《Low level audio players》中粘贴的播放wave的代码。

在下面的代码中,你可以看到,我将实例化Timer对象的方法放在我自己的初始化方法InitializeTimer()中,这个方法将被类构造调用。并且我创建了两个方法,inputToSeconds()和secondsToTime()用来将字符串格式的时间格式转换为正型,以及一个反处理过程。这些方法只是用来帮助我们在TextBox控件中显示日期格式,这在整个应用的结构中,并不十分重要。其他的那些代码,是标准的Visual Studio.NET为Win Form程序生成的样板文件。

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Threading;

using System.Timers;

using System.IO;

using System.Reflection;

namespace timerAlarm

{

public class TimerForm : System.Windows.Forms.Form

{

//Controls and Components

private System.Windows.Forms.TextBox timerInput;

private System.Windows.Forms.Button StartButton;

private System.Windows.Forms.Button ResetButton;

private System.ComponentModel.IContainer components;

//Timer and associated variables

private System.Timers.Timer timerClock = new System.Timers.Timer();

private int clockTime = 0;

private int alarmTime = 0;

public TimerForm()

{

InitializeComponent();

InitializeTimer();

}

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

#endregion

public void InitializeTimer()

{

this.timerClock.Elapsed += new ElapsedEventHandler(OnTimer);

this.timerClock.Interval = 1000;

this.timerClock.Enabled = true;

}

[STAThread]

static void Main()

{

Application.Run(new TimerForm());

}

private void TimerForm_Resized(object sender, System.EventArgs e)

{

if( this.WindowState == FormWindowState.Minimized )

{

this.Hide();

}

}

private void StartButton_Click(object sender, System.EventArgs e)

{

this.clockTime = 0;

inputToSeconds( this.timerInput.Text );

}

private void ResetButton_Click(object sender, System.EventArgs e)

{

try

{

this.clockTime = 0;

this.alarmTime = 0;

this.timerInput.Text = "00:00:00";

}

catch( Exception ex )

{

MessageBox.Show("ResetButton_Click(): " + ex.Message );

}

}

public void OnTimer(Object source, ElapsedEventArgs e)

{

try

{

this.clockTime++;

int countdown = this.alarmTime - this.clockTime ;

if( this.alarmTime != 0 )

{

this.timerInput.Text = secondsToTime(countdown);

}

//Sound Alarm

if( this.clockTime == this.alarmTime )

{

MessageBox.Show("Play Sound");

}

}

catch( Exception ex )

{

MessageBox.Show("OnTimer(): " + ex.Message );

}

}

private void inputToSeconds( string timerInput )

{

try

{

string[] timeArray = new string[3];

int minutes = 0;

int hours = 0;

int seconds = 0;

int occurence = 0;

int length = 0;

occurence = timerInput.LastIndexOf(":");

length = timerInput.Length;

//Check for invalid input

if( occurence == -1 || length != 8 )

{

MessageBox.Show("Invalid Time Format.");

ResetButton_Click( null, null );

}

else

{

timeArray = timerInput.Split(':');

seconds = Convert.ToInt32( timeArray[2] );

minutes = Convert.ToInt32( timeArray[1] );

hours = Convert.ToInt32( timeArray[0] );

this.alarmTime += seconds;

this.alarmTime += minutes*60;

this.alarmTime += (hours*60)*60;

}

}

catch( Exception e )

{

MessageBox.Show("inputToSeconds(): " + e.Message );

}

}

public string secondsToTime( int seconds )

{

int minutes = 0;

int hours = 0;

while( seconds >= 60 )

{

minutes += 1;

seconds -= 60;

}

while( minutes >= 60 )

{

hours += 1;

minutes -= 60;

}

string strHours = hours.ToString();

string strMinutes = minutes.ToString();

string strSeconds = seconds.ToString();

if( strHours.Length < 2 )

strHours = "0" + strHours;

if( strMinutes.Length < 2 )

strMinutes = "0" + strMinutes;

if( strSeconds.Length < 2 )

strSeconds = "0" + strSeconds;

return strHours + ":" + strMinutes + ":" + strSeconds;

}

}

}

代码参考

实际的执行代码比上面的要多,但demo中关于播放wave音频的代码是取自Ianier Munoz关于A low-level audio player in C#一文,顺便一提,Ianier Munoz编写的播放wave的这个类十分便于使用和重用。

总结

这个应用程序演示了timer在实际环境中的一个简单应用,仅仅使用了一些简单的基础知识来创建一个简单的应用,我希望在我之后有人能给出它的更多更好的用法。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有