Invik
Invik

Reputation: 48

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

My first question here. I hope that I won't make any blunders...

I'm a beginner self-taught hobbyist C# programmer (books, googling, msdn, and writing apps for myself). It might be something stupid or obvious that I forgot but I can't figure this problem by myself. Google failed me, so I need a little bit help and guidance.

I'm stuck on this from 8 hours and now in the middle of the night I'm asking for help. All hints and advises are welcome.

Thank you in advance!

Description

I have 2 forms. OnButtomclick Form A is running Form B where the problem lies. In Form B among all other things (controls etc.) I have a timer run in a Thread which is sending a command scfile = Ncom.sendcmd("gsc|||" +DWith+"|||"+DHeigh+"$", false) to server every x seconds. Timer and thread are fired after Form B opens. On form B I have one control(combobox) which is to eventually change value of myTimer.Interval and other one(Tickbar) to control values of DWith and DHeigh

My Problem

If I close that Form B and open it again n times then every timer "tick" the command is executed n+1 times. I can't figure this out. Also I noticed that if I open Form B, change DWith and DHeigh to say 1 and 3, close the form, open it again, change values to 2 and 5, close and open it again, every timer tick I'll get executed:

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

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

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

instead of only :

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

where 8 and 9 are default values for command after opening form B

What I tried

I though that maybe thread did not finished or the timer wasn't stopped. So in My FormClosing event RDFormClosing(object sender, FormClosingEventArgs e) I tried everything including Thread.Abort(); (I know that aborting/killing thread is a bad idea) but still, no luck.

Out of curiosity I tried to change myTimer.AutoReset value to false. Still the same problem. Only that it wasn't repeated every x seconds.

My question

What did I miss? Why this is happening even though I stop timer and kill thread before closing Form B? How to fix it?

Source

Form B is opened like this from 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 I have something like 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
}

Upvotes: 3

Views: 2106

Answers (1)

Kirk Woll
Kirk Woll

Reputation: 77546

Your timer is static and your form is not. Thus each time you register Elapsed it aggregates. As someone else had commented, removing the event in form closing should take care of it.

Upvotes: 3

Related Questions