Introduction:
When I used the computer, I was always so concentrated, so that when I passed the "for a while" to see time, I found out three hours, but I didn't realize it! So I decided to use the C # technology I learned from Code Project to create a simple application - using the Timer object to count down a time set by my own, and I will loop play a WAVE music until you re-set the Timer control. .
Timer object foundation
First of all, you have to know, using the Timer object you need to access the following namespace:
Using system.threading;
Using system.timers;
Next, introduce the point of creating a Timer and delegate the ELAPSED event setting event for this Timer object.
Create a Timer object first, here I define the Timer I use for TimerClock. Next, set the ELAPSED event delegation. When the event is triggered, the specified delegation will be called, here I define the delegation name IntiMer ().
Next, set the interval property, using millisecond value indicating the interval that the ELAPSED event is called, which means that when I define the Interval property of 1000 milliseconds, I defined the delegated turnMer () will be called once every 1000 milliseconds, or Say it is every 1 second.
Finally, you need to set the enabled attribute as true to start this Timer object. Next, the rest is just a small problem - create a delegate, call when the ELAPSED property of this Timer object is triggered. If you have not used delegation before, don't worry, they are easy to use, just create a method to receive some variables that suits you capture events.
For the ELAPSED event, this delegate needs to receive a normal object and an ElapSedEventArgs object.
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
}
Use the Timer control in the alarm program
Ok, introduce these foundations, now let's see the code in practical applications. Note that the code that does not include playing WAVE music and display minimizing icons, you can see in that demo project, basically I am pasted from the "Low Level Audio Players" written directly from Jow Blow. Wave code.
In the code below, you can see that I will instantiate the Timer object to put in my own initialization method Initializetimer (), this method will be called by the class constructor. And I created two ways, INPUTTOSECONDS () and secondstotime () were used to convert the time format of the string format into a positive, and an anti-processing process. These methods are only used to help us display the date format in the TextBox control, which is not very important in the entire application structure. Other codes, is the standard Visual Studio.net for the model file generated by the Win Form program.
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 ()
{
InitializationComponent ();
Initializetimer ();
}
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
#ndregion
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 vidinput (String TimerInput)
{
Try
{
String [] TimeArray = New String [3];
INT minutes = 0;
INT Hours = 0;
INT seconds = 0;
INT occupurence = 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 strhouse = 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;
}
}
}
Code reference
The actual execution code is much higher than the above, but the code about playing WAVE audio is taken from the Ianier Munoz About A Low-Level Audio Player In C #, by the way, this class written by Ianier Munoz is very easy to Use and reuse.
to sum up
This app demonstrates a simple application in the actual environment, just use some simple basic knowledge to create a simple app, I hope someone can give more better usage after I will give it.