Download presentation
Presentation is loading. Please wait.
Published byTracy Neal Modified over 9 years ago
1
Events and Timers
2
Event handlers ScratchC#/VS 2 Script == event handler
3
How do you create an event handler? Three Requirements (code examples on next slide): 1.Event handler Method 2.Constructor to create measurement object 3.Call to request event Coding Shortcuts: The “+=“ operator It means you’re adding an event handler to a list Once you type += you can let VS do the work after that. How?, 3
4
Event Handler Example Event handler method, global variables previously defined: void temperatureHumidity_MeasurementComplete(TemperatureHumidity sender, double temperature, double relativeHumidity) { temp = temperature; humid = relativeHumidity; } Event handler constructor: temperatureHumidity.MeasurementComplete += new TemperatureHumidity.MeasurementCompleteEventHandler(temperatureHumidity_ MeasurementComplete); //define the event handler In a timer loop – call to request data from sensor: temperatureHumidity.RequestMeasurement(); //request info
5
Timer Loops Why use a timer Loop? C#.NET programs sometimes need to wait a certain amount of time before carrying out the rest of the code. For example, a program can read from sensor faster then the sensor can give data. This will lead to inaccurate or random data readings. Timer versus thread.sleep. Thread sleep will stop the process (on that thread), which in our case is the main thread. Timers let other processes to continue, in our case events for sensors.
6
Simple Timer Example public partial class Program { bool IsLedOn = false; // This method is run when the mainboard is powered up or reset. void ProgramStarted() { // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging. Debug.Print("Program Started"); GT.Timer timer = new GT.Timer(1000); // every second (1000ms) timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); timer.Start(); oledDisplay.SimpleGraphics.DisplayTextInRectangle("Hello, Gadgeteer!", 2, 2, 100, 30, GT.Color.Red, Resources.GetFont(Resources.FontResources.NinaB)); Debug.Print("Timer Started"); } void timer_Tick(GT.Timer timer) { Mainboard.SetDebugLED(!IsLedOn); IsLedOn = !IsLedOn; }
7
Another Simple Timer Example public partial class Program { GT.Timer ledBlinkTimer = new GT.Timer(1000); void ProgramStarted() { button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed); ledBlinkTimer.Tick += new GT.Timer.TickEventHandler(ledBlinkTimer_Tick); ledBlinkTimer.Start(); } void ledBlinkTimer_Tick(GT.Timer timer) { if (button.IsLedOn == true) { button.TurnLEDOff(); } else button.TurnLEDOn(); } void button_ButtonPressed(Button sender, Button.ButtonState state) { Debug.Print("Button Pressed"); }
8
Timer and TimeSpan Classes Timespan to create duration – TimeSpan(Int32, Int32, Int32, Int32, Int32) Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds. TimeSpan(Int32, Int32, Int32, Int32, Int32) Timer to create constructor and event handling – Timer() Initializes a new instance of the Timer class, and sets all the properties to their initial values. Timer() http://msdn.microsoft.com/en-us/library/system.timespan.aspx http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
9
Time Span and Timer Code Examples Timer Example #2: using System; public class PortChat { public static System.Timers.Timer _timer; public static void Main() { _timer = new System.Timers.Timer(); _timer.Interval = 5000; _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed); _timer.Enabled = true; Console.ReadKey(); } static void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //Do Your calls within this loop. } TimeSpan Example: using System; class Program { static void Main() { // Use TimeSpan constructor to specify: Days, hours, minutes, seconds, milliseconds. The TimeSpan returned has those values. TimeSpan span = new TimeSpan(1, 2, 0, 30, 0); Console.WriteLine(span); } } TimeSpan constructor Example #1 TimeSpan due = new TimeSpan(0,0,0,0); TimeSpan period = new TimeSpan(0,0,0,5); Timer timer = new Timer(myTimerCallback, null, //info to pass to into the called method due, // Amount of time to wait before starting period); //Interval of time between calks void myTimerCallback(object state) //do your calls within this loop.
10
Example Timer Project http://www.classroomthink.com/index.php/gad geteer/17-gadgeteer-projects/12-gadgeteer-its- a-matter-of-time http://www.classroomthink.com/index.php/gad geteer/17-gadgeteer-projects/12-gadgeteer-its- a-matter-of-time http://mikedodaro.net/2012/03/19/net- gadgeteer-motor-control-with-potentiometer/ http://mikedodaro.net/2012/03/19/net- gadgeteer-motor-control-with-potentiometer/
11
More Timer examples using System.Threading; using Microsoft.SPOT; namespace change_this_to_your_namespace { public class Program { class OurClass { public int x; } static void RunMe(object o) { OurClass cls = (OurClass)o; Debug.Print("From timer!"); Debug.Print("Value: " + cls.x.ToString()); } public static void Main() { OurClass cls = new OurClass(); cls.x = 5; Timer MyTimer =new Timer(new TimerCallback(RunMe), cls, 5000, 1000); Debug.Print( "The timer will fire in 5 seconds and then fire periodically every 1 second"); Thread.Sleep(Timeout.Infinite); } } }
12
List of Visual Studio Icons http://msdn.microsoft.com/en-us/library/y47ychfe.aspx
13
Another Simple Timer Example public partial class Program { bool IsLedOn = false; // This method is run when the mainboard is powered up or reset. void ProgramStarted() { // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging. Debug.Print("Program Started"); GT.Timer timer = new GT.Timer(1000); // every second (1000ms) timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); timer.Start(); oledDisplay.SimpleGraphics.DisplayTextInRectangle("Hello, Gadgeteer!", 2, 2, 100, 30, GT.Color.Red, Resources.GetFont(Resources.FontResources.NinaB)); Debug.Print("Timer Started"); } void timer_Tick(GT.Timer timer) { Mainboard.SetDebugLED(!IsLedOn); IsLedOn = !IsLedOn; }} }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.