Csharp/C Sharp/Development Class/Timer
Содержание
Control Modifier
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace ControlModifier
{
/// <summary>
/// Summary description for ControlModifier.
/// </summary>
public class ControlModifier : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public ControlModifier()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
System.Timers.Timer t = new System.Timers.Timer(10000);
t.Elapsed += new System.Timers.ElapsedEventHandler(time);
t.Start();
}
void time(object sender, System.Timers.ElapsedEventArgs e)
{
MessageBox.Show(ControlModifier.ModifierKeys.ToString());
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ruponents = new System.ruponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "ControlModifier";
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ControlModifier());
}
}
}
Demonstrates using the System.Threading.Timer object
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// ThrdTime.cs -- Demonstrates using the System.Threading.Timer object.
//
// Compile this program with the following command line:
// C:>csc ThrdTime.cs
using System;
using System.Windows.Forms;
using System.Threading;
namespace nsDelegates
{
public class ThrdTime
{
static int countdown = 10;
static System.Threading.Timer timer;
static public void Main ()
{
// Create the timer callback delegate.
System.Threading.TimerCallback cb = new System.Threading.TimerCallback (ProcessTimerEvent);
// Create the object for the timer.
clsTime time = new clsTime ();
// Create the timer. It is autostart, so creating the timer will start it.
timer = new System.Threading.Timer (cb, time, 4000, 1000);
// Blessed are those who wait.
MessageBox.Show ("Waiting for countdown", "Text");
}
// Callback method for the timer. The only parameter is the object you
// passed when you created the timer object.
private static void ProcessTimerEvent (object obj)
{
--countdown;
// If countdown is complete, exit the program.
if (countdown == 0)
{
timer.Dispose ();
Environment.Exit (0);
}
string str = "";
// Cast the obj argument to clsTime.
if (obj is clsTime)
{
clsTime time = (clsTime) obj;
str = time.GetTimeString ();
}
str += "\r\nCountdown = " + countdown;
MessageBox.Show (str, "Timer Thread");
}
}
// Define a class to use as the object argument for the timer.
class clsTime
{
public string GetTimeString ()
{
string str = DateTime.Now.ToString ();
int index = str.IndexOf(" ");
return (str.Substring (index + 1));
}
}
}
Demonstrates using the System.Timers.Timer class 2
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
// Timer.cs -- demonstrates using the System.Timers.Timer class.
//
// Compile this program with the following command line:
// C:>csc Timer.cs
using System;
using System.Windows.Forms;
using System.Timers;
namespace nsDelegates
{
public class TimerAndDialog
{
static int countdown = 10;
static System.Timers.Timer timer;
static public void Main ()
{
// Create the timer object.
timer = new System.Timers.Timer (1000);
// Make it repeat. Setting this to false will cause just one event.
timer.AutoReset = true;
// Assign the delegate method.
timer.Elapsed += new ElapsedEventHandler(ProcessTimerEvent);
// Start the timer.
timer.Start ();
// Just wait.
MessageBox.Show ("Waiting for countdown", "Text");
}
// Method assigned to the timer delegate.
private static void ProcessTimerEvent (Object obj, ElapsedEventArgs e)
{
--countdown;
// If countdown has reached 0, it"s time to exit.
if (countdown == 0)
{
timer.Close();
Environment.Exit (0);
}
// Make a string for a new message box.
string sigtime = e.SignalTime.ToString ();
string str = "Signal time is " + sigtime.Substring (sigtime.IndexOf(" ") + 1);
str += "\r\nCountdown = " + countdown;
// Show a message box.
MessageBox.Show (str, "Timer Thread");
}
}
}
Digital Clock with Date
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);
}
}
illustrates the use of the Timer class
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example14_5.cs illustrates the use of the Timer class
*/
using System;
using System.Threading;
public class Example14_5
{
// the CheckTime method is called by the Timer
public static void CheckTime(Object state)
{
Console.WriteLine(DateTime.Now);
}
public static void Main()
{
// create the delegate that the Timer will call
TimerCallback tc = new TimerCallback(CheckTime);
// create a Timer that runs twice a second, starting in one second
Timer t = new Timer(tc, null, 1000, 500);
// Wait for user input
Console.WriteLine("Press Enter to exit");
int i = Console.Read();
// clean up the resources
t.Dispose();
t = null;
}
}
Interval, Tick, Stop
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();
}
}
Simple Clock
using System;
using System.Drawing;
using System.Windows.Forms;
class SimpleClock: Form
{
public static void Main()
{
Application.Run(new SimpleClock());
}
public SimpleClock()
{
Timer timer = new Timer();
timer.Tick += new EventHandler(TimerOnTick);
timer.Interval = 1000;
timer.Start();
}
private void TimerOnTick(object sender, EventArgs ea)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pea)
{
StringFormat strfmt = new StringFormat();
strfmt.Alignment = StringAlignment.Center;
strfmt.LineAlignment = StringAlignment.Center;
pea.Graphics.DrawString(DateTime.Now.ToString("F"),
Font, new SolidBrush(ForeColor),
ClientRectangle, strfmt);
}
}
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]);
}
}
Using TimerCallback
using System;
using System.Threading;
class TimePrinter {
static void PrintTime(object state) {
Console.WriteLine("Time is: {0}, Param is: {1}", DateTime.Now.ToLongTimeString(), state.ToString());
}
static void Main(string[] args) {
TimerCallback timeCB = new TimerCallback(PrintTime);
Timer t = new Timer(
timeCB, // The TimerCallback delegate type.
"Hi", // Any info to pass into the called method.
0, // Amount of time to wait before starting.
1000); // Interval of time between calls.
}
}