andonova
andonova

Reputation: 3

My Timer won't tick c#

My timer won't tick, I've tried with printing to check if the timer is started, it starts but it never ticks. Here's the code:

   public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
        timer = new Timer();
        timer.Enabled = true;
        timer.Interval = 50;
        timer.Tick += new EventHandler(timer_Tick);
        //Console.WriteLine("STARTING TIMER");
        timer.Start();
        NewFile();

    }

    private void timer_Tick(object sender, EventArgs e)
    {
        MessageBox.Show("TIMER TICKS");

        doc.MoveBalls(leftX, topY, width, height);
        doc.CheckCollision();
        Invalidate(true);

        Console.WriteLine("Moving2");
    }

Upvotes: 0

Views: 787

Answers (3)

Asim Khan
Asim Khan

Reputation: 572

Apart from the answers given above, you may also use a thread of Timer which takes callback and a state object to keep your working thread safe like below

After you include the library before your namespace

using System.Threading;

// your namespace

        private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);

        private Timer _timer = new Timer(CallBakFunction, null, _updateInterval, _updateInterval);
        private void CallBakFunction(object state)
        {

        }

In your case, this can be done as follows:

private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(10);
private Timer _timer ;
public Form1()
{
    InitializeComponent();
    this.DoubleBuffered = true;
  _timer = new Timer(timer_Tick, null, _updateInterval, _updateInterval);

    NewFile();

}
      private void timer_Tick(object state)
{
    MessageBox.Show("TIMER TICKS");

    doc.MoveBalls(leftX, topY, width, height);
    doc.CheckCollision();
    Invalidate(true);

    Console.WriteLine("Moving2");
}

I have updated a final result video for your assistance please view this:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private readonly TimeSpan _updateInterval = TimeSpan.FromSeconds(1);
        private System.Threading.Timer _timer;
        public Form1()
        {
            InitializeComponent(); this.DoubleBuffered = true;
            _timer = new System.Threading.Timer(timer_Tick, null, _updateInterval, _updateInterval);

            NewFile();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void NewFile() { }
        private void timer_Tick(object state)
        {
            MessageBox.Show("TIMER TICKS");

            //doc.MoveBalls(leftX, topY, width, height);
            //doc.CheckCollision();
            //Invalidate(true);

            Console.WriteLine("Moving2");
        }
    }
}

Upvotes: 1

Mostafiz
Mostafiz

Reputation: 7352

Use System.Windows.Forms.Timer to work with winform

   public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
        System.Windows.Forms.Timer timer = new Timer();
        timer.Enabled = true;
        timer.Interval = 50;
        timer.Tick += new EventHandler(timer_Tick);
        //Console.WriteLine("STARTING TIMER");
        timer.Start();

    }

Upvotes: 0

Lennart
Lennart

Reputation: 10333

You enabled the timer before you attach the OnTick event. Setting Enabled to true is basically the same as timer.Start(). Remove the Enabled assignment, or replace the Start() call with it.

Upvotes: 1

Related Questions