Question System.Windows.Forms.Timer issue

pablobhz

Member
Joined
Sep 4, 2018
Messages
6
Programming Experience
1-3
Hello Everyone. This is my first post here.

I'm having a very weird issue using Timers.

My elapsed event isn't firing ; when it fires its completely random, not according to the interval i set.
I already tried to drag the timer from the toolbox to the form, creating it manually on the STA Thread, now i'm trying to create it under Form1.cs ...behavior is the same.
I truly don't know whats going on. It was working a few hours before.


Any help is appreciated. THanks !

Code below:
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;


namespace DI_Plot_2018
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        FileSystemWatcher InputFileWatcher;
        FileProcessor TIFFProcessor;
        Timer timer1;
        LoadInternalSettings InternalSettings;
        int counter;        
        public RadForm1()
        {
            InitializeComponent();
            InternalSettings = new LoadInternalSettings();
            counter = Int32.Parse(InternalSettings.Timer);
            timer1 = new Timer();                        
            TIFFProcessor = new FileProcessor();            
            LoadControls(InternalSettings);                        
        }
        private void TimerStart()
        {
            timer1.Enabled = true;
            timer1.Interval = counter * 1000;
            timer1.Tick += new EventHandler(SecondElapsed);
            timer1.Start();
        }
        private void SecondElapsed(object sender, EventArgs e)
        {
            counter--;
            TimerTextBox.Text = counter.ToString();
            if (counter <= 0)
            {
                TIFFProcessor.TriggerWork();
                TimerReset();
            }
            
        }


        private void FileCreated(Object sender, FileSystemEventArgs e)
        {
            JobDetails jobInfo = new JobDetails(TIFFProcessor.fileNamesQueue.Dequeue());
            if (TIFFProcessor.JobFilesAndInfoDictionary.ContainsKey(jobInfo.JobNameWithoutColor))
            {


                TIFFProcessor.JobFilesAndInfoDictionary[jobInfo.JobNameWithoutColor].Add(e.FullPath);
            }
            else
            {
                TIFFProcessor.JobFilesAndInfoDictionary.Add(jobInfo.JobNameWithoutColor, new List<string>() { e.FullPath });
            }
            TimerReset();


        }
        private void TimerReset()
        {
            timer1.Stop();
            TimerStart();
        }


        private void ButtonChangedEventHandler(object sender, EventArgs e)
        {
            Telerik.WinControls.UI.RadButton bt = sender as Telerik.WinControls.UI.RadButton;
            string buttonName = (String)bt.Name;
            SwitchButtonImage buttonImageSwitch;
            switch (buttonName)
            {
                case "StartButton":
                    {
                        InputFileWatcher = new FileSystemWatcher(InputFolderTextBox.Text);
                        InputFileWatcher.Created += new FileSystemEventHandler(FileCreated);
                        InputFileWatcher.Filter = "*.tif";
                        InputFileWatcher.EnableRaisingEvents = true;                                          
                        StopButton.Enabled = true;
                        StartButton.Enabled = false;
                        TimerStart();
                        break;
                    }
                case "StopButton":
                    {
                        InputFileWatcher.EnableRaisingEvents = false;
                        timer1.Stop();
                        StopButton.Enabled = false;
                        StartButton.Enabled = true;
                        break;
                    }
                case "ImageRotationButton":
                    {
                        buttonImageSwitch = new SwitchButtonImage(ImageRotationButton, "ImageRotationIndex");
                        break;
                    }
                case "ImageOrientationButton":
                    {
                        buttonImageSwitch = new SwitchButtonImage(ImageOrientationButton, "ImageOrientationIndex");
                        break;
                    }
                case "InputFolderBrowseButton":
                    {
                        InputFolderTextBox.Text = new ItemSaveToConfig.SelectFolderSaveProperty(InputFolderTextBox.Text, "Select the input folder", "InputFolder").folderSelect;
                        break;
                    }
                case "OutputFolderBrowseButton":
                    {
                        OutputFolderTextBox.Text = new ItemSaveToConfig.SelectFolderSaveProperty(OutputFolderTextBox.Text, "Select the output folder", "OutputFolder").folderSelect;
                        break;
                    }
            }
        }
        private void LoadControls(LoadInternalSettings Settings)
        {
            InputFolderTextBox.Text = InternalSettings.InputFolder;
            OutputFolderTextBox.Text = InternalSettings.OutputFolder;
            NumberOfZonesTextBox.Text = InternalSettings.NumberOfZones;
            FirstZoneWidthTextBox.Text = InternalSettings.FirstZoneWidth;
            LastZoneWidthTextBox.Text = InternalSettings.LastZoneWidth;
            ZoneAreaWidthTextBox.Text = InternalSettings.ZoneAreaWidth;
            ZoneAreaHeightTextBox.Text = InternalSettings.ZoneAreaHeight;
            ZoneWidthTextBox.Text = InternalSettings.ZoneWidth;
            TimerTextBox.Text = InternalSettings.Timer;
            ZoneWidthTextBox.Text = InternalSettings.ZoneWidth;
            var RotatePreview = new SwitchButtonImage(ImageRotationButton, "ImageRotationIndex");
            var OrientationPreview = new SwitchButtonImage(ImageOrientationButton, "ImageOrientationIndex");
        }


        private void TextBoxChangedEventHandler(object sender, EventArgs e)
        {
            Telerik.WinControls.UI.RadTextBox TxtBx = sender as Telerik.WinControls.UI.RadTextBox;
            string TxtName = TxtBx.Name;
            switch (TxtName)
            {
                case "NumberOfZonesTextBox":
                    {
                        NumberOfZonesTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(NumberOfZonesTextBox.Text, "NumberOfZones", false).TextToApply;
                        break;
                    }
                case "ZoneWidthTextBox":
                    {
                        ZoneWidthTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(ZoneWidthTextBox.Text, "ZoneWidth", false).TextToApply;
                        break;
                    }
                case "FirstZoneWidthTextBox":
                    {
                        ZoneWidthTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(FirstZoneWidthTextBox.Text, "FirstZoneWidth", false).TextToApply;
                        break;
                    }
                case "LastZoneWidthTextBox":
                    {
                        ZoneWidthTextBox.Text = new ItemSaveToConfig.ChangeAndSaveTextBox(LastZoneWidthTextBox.Text, "LastZoneWidth", false).TextToApply;
                        break;
                    }
            }


        }
        private void RadForm1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.Timer = TimerTextBox.Text;
            Properties.Settings.Default.Save();
            //WaitingTimer.Stop();
            Environment.Exit(0);
        }
    }
}
 
Every time you call TimerStart you are registering an event handler. That means that you will end up with the same method handling the Tick event multiple times, so the method will be executed multiple times on each Tick. I would suggest that you change this:
        public RadForm1()
        {
            InitializeComponent();
            InternalSettings = new LoadInternalSettings();
            counter = Int32.Parse(InternalSettings.Timer);
            timer1 = new Timer();                        
            TIFFProcessor = new FileProcessor();            
            LoadControls(InternalSettings);                        
        }
        private void TimerStart()
        {
            timer1.Enabled = true;
            timer1.Interval = counter * 1000;
            timer1.Tick += new EventHandler(SecondElapsed);
            timer1.Start();
        }

to this:
        public RadForm1()
        {
            InitializeComponent();
            InternalSettings = new LoadInternalSettings();
            counter = Int32.Parse(InternalSettings.Timer);
            timer1 = new Timer();
            timer1.Tick += new EventHandler(SecondElapsed);                        
            TIFFProcessor = new FileProcessor();            
            LoadControls(InternalSettings);                        
        }
        private void TimerStart()
        {
            timer1.Interval = counter * 1000;
            timer1.Start();
        }

Notice that I have also removed the line that sets the Enabled property to 'true'. Setting Enabled to 'true' and 'false' is functionally equivalent to calling Start and Stop, so there's no reason to do both. Also, you should not be starting the Timer before changing the Interval.

If you were to add the Timer in the designer then you could generate and register the event handler via the Properties window and this issue would never have arisen. I assume that that's possible using a RadForm. I recommend always using the designer when possible.
 
Thanks for your reply ! It worked =D
And i understood what was going on. Next time i'll drag the control from the Toolbox

Really thank you
 
Next time i'll drag the control from the Toolbox

Just a small thing but it's not actually a control. It's a component. All that's required to be used in the WinForms designer is to implement the IComponent interface. The Component class provides a concrete implementation and then other classes normally inherit that. The Control class inherits Component and adds all the UI stuff, then control classes inherit that. The Timer class inherits Component directly. It has no UI so it doesn't need to inherit Control.
 

Latest posts

Back
Top Bottom