c# - Why System.Timers.Timer elapsed is firing multiple times after closing and reopening of the form -


my first question here. hope won't make blunders...

i'm beginner self-taught hobbyist c# programmer (books, googling, msdn, , writing apps myself). might stupid or obvious forgot can't figure problem myself. google failed me, need little bit , guidance.

i'm stuck on 8 hours , in middle of night i'm asking help. hints , advises welcome.

thank in advance!

description

i have 2 forms. onbuttomclick form running form b problem lies. in form b among other things (controls etc.) have timer run in thread sending command scfile = ncom.sendcmd("gsc|||" +dwith+"|||"+dheigh+"$", false) server every x seconds. timer , thread fired after form b opens. on form b have 1 control(combobox) change value of mytimer.interval , other one(tickbar) control values of dwith , dheigh

my problem

if close form b , open again n times every timer "tick" command executed n+1 times. can't figure out. noticed if open form b, change dwith , dheigh 1 , 3, close form, open again, change values 2 , 5, close , open again, every timer tick i'll executed:

scfile = ncom.sendcmd("gsc|||" +1+"|||"+3+"$", false);

scfile = ncom.sendcmd("gsc|||" +2+"|||"+5+"$", false);

scfile = ncom.sendcmd("gsc|||" +8+"|||"+9+"$", false);

instead of :

scfile = ncom.sendcmd("gsc|||" +8+"|||"+9+"$", false);

where 8 , 9 default values command after opening form b

what tried

i though maybe thread did not finished or timer wasn't stopped. in formclosing event rdformclosing(object sender, formclosingeventargs e) tried including thread.abort(); (i know aborting/killing thread bad idea) still, no luck.

out of curiosity tried change mytimer.autoreset value false. still same problem. wasn't repeated every x seconds.

my question

what did miss? why happening though stop timer , kill thread before closing form b? how fix it?

source

form b opened form a:

void buttonsdeclick(object sender, system.eventargs e) {     rd form = new rd(ncom);     form.show();     form.closed += new system.eventhandler(desktopformclosed); } 

in form b have this:

public partial class rd : form {         public networkcom ncom;     string scfile;     static system.timers.timer  mytimer = new system.timers.timer(1000);     static bool exitflag = false;     thread displayupdater;     public rd(networkcom _ncom)     {         // other code         ncom = _ncom;         displayupdater = rundisplay();     }      public thread rundisplay()     {         exitflag = false;         thread updatethread = new thread(() => updatescreen());         updatethread.name = "display";         updatethread.start();         return updatethread;                 }      public void updatescreen()     {         mytimer.autoreset = true;         mytimer.elapsed += new elapsedeventhandler(timereventprocessor);             mytimer.interval = 1000;             mytimer.start();             while(exitflag == false)              {                 application.doevents();             }      }      private void timereventprocessor(object myobject,eventargs myeventargs)          {         scfile = ncom.sendcmd("gsc|||" +dwith+"|||"+dheigh+"$", false);     }          void rdformclosing(object sender, formclosingeventargs e)     {         exitflag = true;         mytimer.stop();         mytimer.close();         displayupdater.abort();     }     // more inessential code } 

your timer static , form not. each time register elapsed aggregates. else had commented, removing event in form closing should take care of it.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -