Attaching custom methods
-
Hi, im new to C# and have a problem when i must assign methods to objects i create in execution mode. while in design mode u can create a timer and then double click on it and there u go, but how can i attach a method to a timer i have created in execution time?
using System.Timers; Timer t = new timer(30000); t.enabled=true; //but how i tell the timer what method or event to raise when its interval elapses?
Ty for ur time. -
Hi, im new to C# and have a problem when i must assign methods to objects i create in execution mode. while in design mode u can create a timer and then double click on it and there u go, but how can i attach a method to a timer i have created in execution time?
using System.Timers; Timer t = new timer(30000); t.enabled=true; //but how i tell the timer what method or event to raise when its interval elapses?
Ty for ur time. -
Hi, im new to C# and have a problem when i must assign methods to objects i create in execution mode. while in design mode u can create a timer and then double click on it and there u go, but how can i attach a method to a timer i have created in execution time?
using System.Timers; Timer t = new timer(30000); t.enabled=true; //but how i tell the timer what method or event to raise when its interval elapses?
Ty for ur time.Declare your Timer [code] System.Timers.Timer myTimer = new System.Timers.Timer(1000); // 1000 is the Interval used by the Timer in ms, i.e. the time between two "Timer.Elapsed" Events [/code] Then add an EventHandler [code] myTimer.Elapsed +=new System.Timers.ElapsedEventHandler(myTimer_Elapsed); // Note: In VS.NET 2003, as soon as you type the +=, VS will ask you to press // TAB. If you do, it will code-complete everything else, even the Delegate's // body if you press TAB again. [/code] Now, on each Elapsed-Event, the Delegate will be executed [code] //If you are not using VS.NET, this is what the delegate should look like: private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { } [/code]
-
Declare your Timer [code] System.Timers.Timer myTimer = new System.Timers.Timer(1000); // 1000 is the Interval used by the Timer in ms, i.e. the time between two "Timer.Elapsed" Events [/code] Then add an EventHandler [code] myTimer.Elapsed +=new System.Timers.ElapsedEventHandler(myTimer_Elapsed); // Note: In VS.NET 2003, as soon as you type the +=, VS will ask you to press // TAB. If you do, it will code-complete everything else, even the Delegate's // body if you press TAB again. [/code] Now, on each Elapsed-Event, the Delegate will be executed [code] //If you are not using VS.NET, this is what the delegate should look like: private void myTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { } [/code]