Weird things with C# events -
i'm learning events , delegates , decided write such console application. program should message me every 3 , 5 seconds. doesn't anything.
i have class workingtimer
:
class workingtimer { private timer _timer = new timer(); private long _working_seconds = 0; public delegate void mydelegate(); public event mydelegate every3seconds; public event mydelegate every5seconds; public workingtimer() { _timer.interval = 1000; _timer.elapsed += _timer_elapsed; _timer.start(); } void _timer_elapsed(object sender, elapsedeventargs e) { _working_seconds++; if (every3seconds != null && _working_seconds % 3 == 0) every3seconds(); if (every5seconds != null && _working_seconds % 5 == 0) every5seconds(); } }
and program:
class program { static void main(string[] args) { workingtimer wt = new workingtimer(); wt.every3seconds += wt_every3seconds; wt.every5seconds += wt_every5seconds; } static void wt_every3seconds() { console.writeline("3 seconds elapsed"); } static void wt_every5seconds() { console.writeline("5 seconds elapsed"); } }
so, when run doesn't anything. tried make same program in windows form application , worked great. difference in timer events elapsed , tick.
what doing wrong?
the program exits @ end of main
function. try adding dummy console.readline()
keep running.
the resulting code be:
static void main(string[] args) { workingtimer wt = new workingtimer(); wt.every3seconds += wt_every3seconds; wt.every5seconds += wt_every5seconds; console.readline(); }
Comments
Post a Comment