Omkar Hendre
Omkar Hendre

Reputation: 33

Timer method not firing

I am using the following code to print a message to the console every 5 minutes but its, but the Timer doesn't seam to be firing. What is wrong?

I want to call the MyMethod() after every 10 seconds but it calls only once

    using System;
using System.Threading;

namespace ThreadProgram
{
    class Program
    {
        private static System.Threading.Timer timer = new System.Threading.Timer((e) =>
        {
            MyMethod();
        }, null, 0, TimeSpan.FromSeconds(10).Milliseconds);

        static void Main(string[] args)
        {
            Console.WriteLine("----Calling my method----");
            Console.ReadLine();
        }

        private static void MyMethod()
        {
            Console.WriteLine("*** Method is executed at {0} ***", DateTime.Now);
            Console.ReadLine();
        }
    }
}

Thanks in advance for your suggestion

Upvotes: 0

Views: 211

Answers (4)

Omkar Hendre
Omkar Hendre

Reputation: 33

Friends i got my answer but thanks to all of you for valuable suggestion changes that i made in my program are as follows which may be useful for future references

static System.Threading.Timer timer = new System.Threading.Timer((e) =>
        {
            MyMethod();
        }, null, 0, TimeSpan.FromSeconds(20).Seconds);

Once again thanks to all

Upvotes: 0

Neel
Neel

Reputation: 11741

you need to enable as well as start your timer as shown below :-

static void Main(string[] args)
        {
            Console.WriteLine("----Calling my method----");
            timer.Start();
            Console.ReadLine();

        }

I just debugged your code it requires your MyMethod to be static else it will not even compile

and signature of the Timer is wrong as TotalMiliseconds returns double and it reuires int so better try below code using .Milliseconds + make your timer static else it would not be accessible in Main method :-

 private static System.Threading.Timer timer = new System.Threading.Timer((e) =>
        {
            MyMethod();
        }, null, 0, TimeSpan.FromMinutes(5).Milliseconds);

Upvotes: 1

S.L.
S.L.

Reputation: 1076

class Program
{
  static Timer timer = new Timer(TimeSpan.FromMinutes(5).Milliseconds);

  static void Main(string[] args)
  {
    Console.WriteLine("----Calling my method----");
    timer.AutoReset = true;
    timer.Elapsed += timer_Elapsed;
    timer.Start();
    Console.ReadLine();
  }

  static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  {
    MyMethod();
  }

  private static void MyMethod()
  {
    Console.WriteLine("*** Method is executed at {0} ***", DateTime.Now);
    Console.ReadLine();
  }
}

Try this

using Timer = System.Timers.Timer;

Upvotes: 0

Matthijs
Matthijs

Reputation: 3170

There are two problems with this code. First is the timer not being started.

Make a call to timer.Start(); in your main-method.

But there is another problem, as your code doesn't even compile. You are trying to call MyMethod from the timer; but this isn't possible as MyMethod isn't static.

Your code would change to the following:

static void Main(string[] args)
{
     Console.WriteLine("----Calling my method----");
     timer.Start();
     Console.ReadLine();
}

private static void MyMethod()
{
    Console.WriteLine("*** Method is executed at {0} ***", DateTime.Now);
    Console.ReadLine();
}

Last, the signature of the timer is wrong. It should be a static timer, and also the last parameter of the timer, period, requires an integer. Timespan.TotalMiliseconds returns a double so you are better off with .Miliseconds:

private static System.Threading.Timer timer = new System.Threading.Timer(state => MyMethod(), null, 0, TimeSpan.FromMinutes(5).Milliseconds);

Upvotes: 2

Related Questions