Csharp/C Sharp by API/System.Timers/Timer — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
| |
Текущая версия на 12:11, 26 мая 2010
Содержание
new Timer()
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
public System.Timers.Timer timer1;
private System.Windows.Forms.Label label1;
public Form1() {
this.timer1 = new System.Timers.Timer();
this.label1 = new System.Windows.Forms.Label();
((System.ruponentModel.ISupportInitialize)(this.timer1)).BeginInit();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.SynchronizingObject = this;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimerElapsed);
//
// label1
//
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
this.label1.Location = new System.Drawing.Point(24, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(224, 48);
this.label1.TabIndex = 0;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 69);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Text = "My Clock";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ruponentModel.ISupportInitialize)(this.timer1)).EndInit();
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
timer1.Interval = 1000;
timer1.Start();
timer1.Enabled = true;
}
private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) {
label1.Text = DateTime.Now.ToString();
}
}
new Timer(new TimerCallback(TimerHandler), null,0)
using System;
using System.Threading;
public class MainClass
{
public static AutoResetEvent A = new AutoResetEvent(false);
public static int index = 0;
public static int Main(){
Timer T = new Timer(new TimerCallback(TimerHandler), null, 5000, 10000);
A.WaitOne();
Console.WriteLine("Main Thread event signaled");
T.Dispose();
return 0;
}
public static void TimerHandler(object state)
{
Console.WriteLine("TimerHandler");
if (index == 5)
A.Set();
index++;
Console.WriteLine(index);
}
}
Timer.Elapsed
using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
class Program {
static int counter = 0;
static string displayString = "This string will appear one letter at a time. ";
static void Main(string[] args) {
Timer myTimer = new Timer(100);
myTimer.Elapsed += new ElapsedEventHandler(WriteChar);
myTimer.Start();
Console.ReadKey();
}
static void WriteChar(object source, ElapsedEventArgs e) {
Console.Write(displayString[counter++ % displayString.Length]);
}
}
Timer.Interval
using System;
using System.Drawing;
using System.Windows.Forms;
class DigitalClock: Form
{
public static void Main()
{
Application.Run(new DigitalClock());
}
public DigitalClock()
{
ResizeRedraw = true;
Timer timer = new Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}
private void TimerOnTick(object obj, EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics grfx = pea.Graphics;
DateTime dt = DateTime.Now;
string strTime = dt.ToString("d") + "\n" + dt.ToString("T");
SizeF sizef = grfx.MeasureString(strTime, Font);
float fScale = Math.Min(ClientSize.Width / sizef.Width,
ClientSize.Height / sizef.Height);
Font font = new Font(Font.FontFamily,
fScale * Font.SizeInPoints);
StringFormat strfmt = new StringFormat();
strfmt.Alignment = strfmt.LineAlignment = StringAlignment.Center;
grfx.DrawString(strTime, font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);
}
}
Timer.Tick
using System;
using System.Drawing;
using System.Windows.Forms;
class CloseInFive: Form
{
public static void Main()
{
Application.Run(new CloseInFive());
}
public CloseInFive()
{
Text = "Closing in Five Minutes";
Timer timer = new Timer();
timer.Interval = 5 * 60 * 1000;
timer.Tick += new EventHandler(TimerOnTick);
timer.Enabled = true;
}
void TimerOnTick(object obj, EventArgs ea)
{
Timer timer = (Timer) obj;
timer.Stop();
timer.Tick -= new EventHandler(TimerOnTick);
Close();
}
}