Hello.
I need to simulate a button click with a Timer. That the action performed by the button, repeats every 60 seconds. I've never use timers, any help?

Dani AI

Generated

Good question, . Calling the click handler from the timer (as showed and noted) works, but a cleaner pattern is to move the actual work into a separate method and call that from both places. It keeps event plumbing separate from business logic and avoids relying on sender/EventArgs being meaningful.

private void RunAction()
{
    // Put the code that the button should perform here.
}

private void button1_Click(object sender, EventArgs e)
{
    RunAction();
}

private void timer1_Tick(object sender, EventArgs e)
{
    // If RunAction can take time, stop first to prevent overlap.
    timer1.Stop();
    try { RunAction(); }
    finally { timer1.Start(); } // Fire again in another 60s.
}

Timer choice matters (building on ’s links). Use System.Windows.Forms.Timer if you want the tick to run on the UI thread. If you switch to System.Timers.Timer or System.Threading.Timer, their callbacks run on a thread-pool thread; any UI updates must be marshaled back to the UI thread, e.g. BeginInvoke(() => RunAction());. That avoids cross-thread exceptions.

If your goal is a true, visual click (as asked), you can also trigger the button itself:

private void timer1_Tick(object sender, EventArgs e)
{
    if (button1.Enabled) button1.PerformClick();
}

Practical tips:

  • Set timer1.Interval = 60000 and start it when the form loads.
  • Guard against reentrancy if RunAction can overlap (the Stop/Start pattern above is simple and effective).
  • Keep long work off the UI thread; if needed, run the heavy part on a background thread and only marshal UI updates back to the form.

Recommended Answers

All 5 Replies

Just call the method that defined for button event into timer event method.

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1_Click( sender, e);
        }

you also could put null for parameters.

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1_Click( null, null);
        }

Better to use null as both arguments, then passing some Time parameters to button (2nd example from above is the one you need). But only in case when you will not need and parameters from EventArguments, or sender - but I doubt you will need).
Else you can create EventArguments manully.

Yes you can create own event arguments. You need to create a class that inherited from EventArgs for example

using System;
namespace EventExample
{
    public class ShipArgs : EventArgs
    {
        private string message;

        public ShipArgs(string message)
        {
            this.message = message;
        }

        // This is a straightforward implementation for 
        // declaring a public field
        public string Message
        {
            get
            {
                return message;
            }
        }
    }
}

Do you just want to simulate the result of a click (ie calling the click event handler)? Or did you want to automate the click (ie the mouse move onscreen and visually depresses the button)? The first one is by far easier than the second one, but both are possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.