180 likes | 361 Views
Advanced Windows Prog. Chapter 10 – The Timer and Time Programming Microsoft Windows with C# - Charles Petzold Presented By: Srinivas B. Shilagani. Timer. Timer is an i/p device that periodically notifies an application when a specified interval time has elapsed.
E N D
Advanced Windows Prog. Chapter 10 – The Timer and Time Programming Microsoft Windows with C# - Charles Petzold Presented By: Srinivas B. Shilagani
Timer • Timer is an i/p device that periodically notifies an application when a specified interval time has elapsed. • The timer triggers an event handler after every specified interval
C# Timers Three classes are defined in • System.Timers • System.Threading • System.Windows.Forms*
Application • Multitasking: It is advisable to return control to windows because of premptive nature of windows. • Maintaining an updated status report. • Implementing an auto save feature. • Animation: Eliminates inconsistencies because of variations in microprocessor speed.
Timer Class • Constructor: Timer timer = new Timer(); • Events Timer.Tick += new EventHandler(TimerOnTick); void TimerOnTick(object obj, EventArgs ea){ … }
Timer Properties Can we always achieve interval granularity? • Windows rounds the interval you specify to the next highest multiple of the period of the OS’s internal clock. • Timer Methods include start() and stop()
DateTime Structure • Namespace: System.DateTime Represents Date and Time in .NET Framework. • Constructors: 7 are available • DateTime(int year, int month, int day) • DateTime(int year, int month, int day, int hour, int minute, int second) • DateTime(int year, int month, int day, int hour, int minute, int second, int msec)
Points to note • Exception is thrown on inconsistency of Date. • Default Calendar used is Gregorian Calendar. • Leap Year is defined as any year divisible by 4 and not by 100 unless it is divisible by 400
DateTime Properties • Most frequently used properties: • Year, Month, Day, Hour, Minute, Second, MilliSecond, DayofWeek, DayofYear, Date • UTC – Coordinated Universal Time • DateTime ToLocalTime() • DateTime ToUniversalTime() • Operators ==, !=, <, >, <=, >=
System.TimeZone • Its an Abstract Class • Static Property :CurrentTimeZone() TimeZone tz=TimeZone.CurrentTimeZone(); How does this work? • Properties
TimeZone Methods • TimeSpan GetUtcOffset(DateTime dt) • DateTime ToLocalTime(DateTime dt) • DateTime ToUniversalTime(DateTime dt) • DaylightTime GetDaylightChanges(int year) • bool IsDaylightSavingTime(DateTime dt) Eg: tz.GetUtcOffset(new DateTime(2002,2,2)) returns –5:00:00. We add this to get EST.
DaylightTime and Ticks • DaylightTime properties • DateTime Start() (April 7 2002 at 2am) • DateTime End() (October 27 2002 2am) • DateTime Delta() • The Tick Count • Number of 100nS since midnight January 1,1 • Constructor: • DateTime(long ticks) • Properties: • long Ticks() – 100nS interval since 1/1/0001. • TimeSpan TimeOfDay() – Ticks since midnight.
TimeSpan • It’s a structure defined in a System namespace that is used to express durations of time in units of 100nS. It represents elapsed time. • Constructor: • TimeSpan(long ticks) • TimeSpan(int hours, int minutes, int seconds) • TimeSpan(int days, int hours, int minutes, int seconds) • It is possible to add Date and TimeSpan.
Calendar • NameSpace: System.Globalization • Calendar is an Abstract class • Constructors • DateTime(int year, int month, int day, Calendar cal) • DateTime(int year, int month, int day, int hr, int min, int sec, Calendar cal) • Methods • int GetYear(DateTime dt) • int GetMonth(DateTime dt) • int GetDayOfMonth(DateTime dt)
Formatting Display • DateTime.ToString(string strFormat, IFormatProvider ifp) • String Formats: series of letters that describe custom formatting. • D - Saturday, June 01, 2002 • f - Saturday, June 01, 2002 4:00 AM • d - Date, f – Full, g – General, m – month/day, t- time, y – year/month.
IFormatProvider • IFormatProvider is an interface. We need a class that represents an instance of a class that implements this interface. Eg: DateTimeFormatInfo • Properties - static • DateTimeFor matInfo CurrentInfo() • DateTimeFormatInfo InvariantInfo()