Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i change the timer interval according to numericupdown value in real time?

Tags:

c#

I have Timer3 tick event inside i set the timer3 interval to the numericupdown value:

private void timer3_Tick(object sender, EventArgs e)
        {
            try
            {
                Image iOLd = this.pictureBox1.Image;
                Image img = Image.FromFile(_files[_indx].FullName);
                trackBar1.Value = _indx;
                label23.Text = _files[_indx].Name;
                this.pictureBox1.Image = img;

                if (iOLd != null)
                    iOLd.Dispose();
                _indx++;

                if (_indx >= _files.Count)
                {
                    _indx = 0;
                    trackBar1.Value = 0;
                }
                timer3.Interval = Convert.ToInt32(numericUpDown1.Value); 
            }
            catch
            {

            }
        }

I also did it in the numericupdown valuechanged event:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            timer3.Interval = Convert.ToInt32(numericUpDown1.Value);
        }

The problem is for example i set the numericupdown value while the program is running to 10000 and its moving very slow then i set at once the value to 1 so instead the timer3 interval to take effect once i changed it to 1 its waiting for another cycle of the 10000 value then the timer3 interval is acting as value 1.

What i want to do is when i change the numericupdown from 10000 to 1 it will change rightaway and not wait for another round of the 10000 value.

like image 671
user1544479 Avatar asked Jul 24 '12 07:07

user1544479


People also ask

What is timer interval in VB net?

The default interval is 100 milliseconds, but the range of possible values is 1 to 2,147,483,647. A timer is typically used in a VB.Net program by creating an event handler for its Tick event (the event handler contains code that will be executed when a Tick event occurs).

What is timer interval in C#?

The Timer class in C# represents a Timer control that executes a code block at a specified interval of time repeatedly. For example, backing up a folder every 10 minutes, or writing to a log file every second. The method that needs to be executed is placed inside the event of the timer.


1 Answers

Supposing you are using a Windows.Forms.Timer then you need to stop the Timer before changing the interval then restart it.

timer3.Stop();
timer3.Interval = Convert.ToInt32(numericUpDown1.Value); 
timer3.Start();

From MSDN

Calling Start after you have disabled a Timer by calling Stop will cause the Timer to restart the interrupted interval. If your Timer is set for a 5000-millisecond interval, and you call Stop at around 3000 milliseconds, calling Start will cause the Timer to wait 5000 milliseconds before raising the Tick event.

like image 180
Steve Avatar answered Oct 19 '22 22:10

Steve