Methods and Timer in C# -


when debug small program step step f10 button, program rational until reaches level timer.elapsed +=. after supposed go call method check(myconn) doesn't! goes myconn.close(); , bounces between these two, closes program of sudden!

i wondering issue comes ... line: timer.elapsed += (timersender, timerevent) => timer_elapsed(timersender, timerevent, myconn);? solution posted on forum in case wanted give in argument myconn timer_elapsed ...

thanks in advance help!

static void main(string[] args) {     // create connection     string connstr = "provider=microsoft.ace.oledb.12.0;data source=c:\\users\\mike\\documents\\database1.mdb;";      oledbconnection myconn = new oledbconnection(connstr);     myconn.open();      inittimer(myconn);      myconn.close(); }  static void inittimer(oledbconnection myconn) {     //set timer     timer timer = new timer();     timer.interval = 2000; // check every 2s (2000ms) if values in database changed     timer.enabled = true; //enable timer, when timer elapses after 2s, performs calculations      timer.elapsed += (timersender, timerevent) => timer_elapsed(timersender, timerevent, myconn); }  static void timer_elapsed(object sender, elapsedeventargs e, oledbconnection myconn) {     check(myconn); // check method have in program takes argument "myconn" } 

after set timer, code continue running. leave inittimer method , close connection. after that, main function exit causing application close. timer terminate then.

in case program not being terminated, timer counting down 2000. when reaches zero, fire event. happens on thread , happens parallel main theat. check connection, obiously closed.

if want main threat wait till event fired, why use timer @ all? why not this?

static void main(string[] args) {     // create connection     string connstr = "provider=microsoft.ace.oledb.12.0;data source=c:\\users\\mike\\documents\\database1.mdb;";      using(oledbconnection myconn = new oledbconnection(connstr))     {         myconn.open();                 {             thread.sleep(2000);              check(myconn);           }         while(somevaluetoindicatetheapplicationcanterminate);     } } 

Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -