tags:

views:

561

answers:

3
+2  Q: 

C# timer issue

Hello I'm currently having an issue with a timer in a program I'm developing. The timer runs and calls methods which retrieve Windows Management Information from remote PC's after a set period of time and repeat this.

The first time the timer calls these all is well, however the second time, after the timer has completed its task, it loops through itself again and the third time it runs it does it 3 times etc. The for loop in the code below works fine its the timer itself.

So any help would be appareciated and if you require any further details please let me know.

Below is my code:

private void tmrStore_Tick(object sender, EventArgs e)
    {
        string ipAdd;
        ipAdd = "127.0.0.1";
        List<TblServer> Server;

        WMIInfo localDB = new WMIInfo("Data Source=|DataDirectory|\\WMIInfo.sdf");

        Server = localDB.TblServer.ToList();

        if (Server.Count == 0)
        {

        }
        else
        {
            for (int counter = 0; counter < Server.Count; counter++)
            {
                CPUStore cpu = new CPUStore();
                cpu.Store(Server[counter].IpAdd);

                HDDStore hdd = new HDDStore();
                hdd.Store(Server[counter].IpAdd);

                MemStore mem = new MemStore();
                mem.Store(Server[counter].IpAdd);

                //delete items over 24 hours old 
            }
        }
+3  A: 

Try disabling the timer before performing the management task, then reenabling:

tmrStore.Enabled = false;
try{
    // do stuff
}finally{
    tmrStore.Enabled = true;
}

The cause of the problem is probably that the body of your timer handler takes longer to execute than your Timer.Ticks value, so your timer events start to stack on top of each other.

You might also consider putting this code in a thread instead of a timer, so that it's independent of your user interface.

Dmitry Brant
What's the cause of the problem and how would this fix it? It's obvious that the problem is somewhere other than the code that he pasted and this answer just smacks of trying random things, unless you do actually have a perfectly good explanation, in which case I apologise.
IRBMe
...it's a perfectly plausible solution if the code takes longer to execute than the interval of the timer.
Dmitry Brant
This might work if the code in the tick event takes so long time that the event is fired again on another thread. He is calling remote computers which can be a slow operation, perhaps longer than the interval of the timer.
Manga Lee
A: 

My first guess is that you are setting your Timer.Tick event in a place that is being executed multiple times. I would try searching for "tmrStore.Tick +=" to see where all the methods are being added to the event.

Totty
A: 

Right I've resolved the issue its because I had a class I was using to write the retrieved information into text boxes and within that I called a new instance of the form to gain access to the text boxes doh!

Thanks for your help though guys no doubt I'll be back soon for some more lol

manemawanna