0

How to use timers to trigger click button event every 3 seconds?

I'm trying to rotate 2 pictures in pictureboxes by triggering the rotate button automaticly using timer but it seems doesnt works. I never used timer before so this is my first time. Anyone know whats wrong with my code or any other code suggestion for it? Thanks

Code I'm using

        private void timer1_Tick(object sender, EventArgs e)
        {
            rotateRightButton_Click(null, null);
            pictureBox1.Refresh();
            pictureBox2.Refresh();
        }
        private void timerStartButton_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
        private void timerStopButton_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }
  • Is it necessary to trigger the Button? Why not just rotating the Picture with CSS like here? https://stackoverflow.com/questions/19126432/rotate-a-div-using-javascript – BrOsCoRe Jan 06 '20 at 08:58
  • To "click" a button you should be using [`rotateRightButton.PerformClick()`](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.button.performclick?view=netframework-4.8) – Matthew Watson Jan 06 '20 at 08:58
  • What's the issue you are facing with your code? You should create a method which rotates the picture and call that method from both `timer1_Tick` and `button_click` event handlers. – Chetan Jan 06 '20 at 08:58
  • Did you set your timer interval? timer.Interval = 3000; ? – Shai Aharoni Jan 06 '20 at 08:59
  • yes I've set the interval to 3000. also method to rotate the pictures and anything inside the pictures is all inside the rotate button. @BrOsCoRe its not only pictures there but also line drawn by coordinates so to make it simple I just want the timer to click the button. – Novan Noviandri Sumarna Jan 06 '20 at 09:04
  • @MatthewWatson tried that just now but still didnt work. will try again tomorrow – Novan Noviandri Sumarna Jan 06 '20 at 09:05
  • `rotateRightButton_Click(null, null);` who is the sender? – Jeroen van Langen Jan 06 '20 at 09:13
  • Did yolu try setting a debug point in timer1_tick and made sure it is hit ? – Anu Viswan Jan 06 '20 at 09:13
  • Better to create a method say `private void RotateRight() { .. }` to do the code in `rotateRightButton` so you can call that method in the `rotateRightButton` click event and in the timer's `Tick` event and elsewhere. Also, as I remember your growing project, you are handling the `Paint` event to draw images and lines, then you should call `pictureBox1.Invalidate()` method to update the drawings instead of `pictureBox1.Refresh()`. –  Jan 06 '20 at 11:42

2 Answers2

1

It's even possible (and more simple) with tasks

public partial class Form1 : Form
{
    // variable to keep track if the timer is running.
    private bool _timerRunning;

    public Form1()
    {
        InitializeComponent();
    }

    private async Task StartTimer()
    {
        // set it to true
        _timerRunning = true;

        while (_timerRunning)
        {
            // call the rotateRightButton_Click (what you want)
            rotateRightButton_Click(this, EventArgs.Empty);
            pictureBox1.Refresh();
            pictureBox2.Refresh();
            // wait for 3 seconds (but don't block the GUI thread)
            await Task.Delay(3000);
        }
    }

    private void rotateRightButton_Click(Form1 form1, EventArgs empty)
    {
       // do your thing
    }

    private async void buttonStart_Click(object sender, EventArgs e)
    {
        // if it's already started, don't start it again.
        if (_timerRunning)
            return;

        // start it.
        await StartTimer();
    }

    private void buttonStop_Click(object sender, EventArgs e)
    {
        // stop it.
        _timerRunning = false;
    }
}
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0
timer1.Interval = 3000; // set interval to 3 seconds and then call Time Elapsed event

timer1.Elapsed += Time_Elapsed;


//Event
private void Time_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
       // will be triggered in every 3 seconds
        rotateRightButton_Click(null, null);
        pictureBox1.Refresh();
        pictureBox2.Refresh();
 }

Hope this helps!

Asım Gündüz
  • 1,257
  • 3
  • 17
  • 42