-1

I tried to identify the issue in below given code. timer is calling the method only for first instance but not calling it after 2500 ms time interval. Is there something that i am missing.Below is my code

public class Program
{
    public void CheckStatus(object stateInfo)
    {
        Console.WriteLine("I am Executed");
    }

    public static void Main(string[] args)
    {
        try
        {
            Program p = new Program();
            Console.WriteLine("Creating timer.\n");
            var stateTimer = new Timer(state => p.CheckStatus(state), null, 0, 2500);
        }

        catch (Exception ex)
        {
            Console.WriteLine("Exception");
        }
    }
}

In above code i am tring to execute the method "CheckStatus" after every 2500 ms and for first execution it should execute immediately (pass parameter as 0) I also tried to replace time value with timespan but it also did not worked for me

Pavan
  • 337
  • 5
  • 23
  • 2
    The timer doesn't restart when it expires. You need to tell it to restart. – gunr2171 Feb 09 '18 at 15:47
  • i don't want to expire timer i should run till my application is running – Pavan Feb 09 '18 at 15:48
  • 2
    Looking at MSDN, https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx Sounds like `AutoReset` property is what you're looking for. – penleychan Feb 09 '18 at 15:49
  • If that's the full extend of `Main`, then the problem is that your program is quiting before you ever had a chance to execute the timer a second time – Matt Burland Feb 09 '18 at 15:53
  • There are 2 basic reasons. 1: there is nothing from stopping the Main() method to exit and thus terminating the program. 2: the local variable is not good enough to prevent the Timer object from getting garbage-collected. – Hans Passant Feb 09 '18 at 15:53
  • @gunr2171 not related - autorestet is default when second parameter is not infinite – Alexei Levenkov Feb 09 '18 at 15:53
  • I originalyl wrote this as a full asnwer, so sorry for the comment spam ahead of time: First, there are 3-5 different Timers in .NET right now. Of wich at least 3 are all called "Timer". And I do not know how many there are in .NET Core and Mono respectively. Their behaviors drastically differ, inlcuding repition, Properties, how the events look and if they can/will raise the TickEvent on an alterante thread. So you really have to look at your timers documentation. – Christopher Feb 09 '18 at 15:58
  • Secondly, you are writing a console application. I generally would not try to learn using timers or Multtiasking outside of a GUI (WinForms or WPF). Consoles have the notorious property to quit rather quickly. And you do not want to have to reinvent the Event Queue of a GUI. – Christopher Feb 09 '18 at 15:58
  • Thridly and only a personal pet-peeve of mine, is your exception handling. You catch fatal exception and swallow them. That is deadly sin of Exception handling. I would advice to read up these two articles that I link a lot: 1. http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx 2. http://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET – Christopher Feb 09 '18 at 15:58

1 Answers1

0

Your program exist before second interval fires. Wait in some way to see callbacks on next intervals. Most common way for console samples is ReadLine (more options - How to stop C# console applications from closing automatically?):

Console.WriteLine("Creating timer.\n");
var stateTimer = new Timer(state => p.CheckStatus(state), null, 0, 2500);
Console.ReadLine();

In case of using Timer even Thread.Sleep(10000); would do as timer callbacks are run on separate thread.

Note: the other standard reason why timer stop firing is timer being garbage collected, but it is not the case in your sample. See System.Threading.Timer not firing after some time for more info.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I can't use a console.readline as i have to perform other task too and it's not expected to have console input. So i created a Task and called thread sleep for 2500 ms and executing in while loop.this solution working for me BUT point is whether it is a good way achieve this? – Pavan Feb 09 '18 at 16:33
  • @Pavan it is your call whether given approach works for your code. If you have reasons to believe your solution have problems - feel free to ask new question (obviously make sure to search for similar posts/articles first). Note that generic "is it good design for my case" questions are not welcome on SO. – Alexei Levenkov Feb 09 '18 at 16:46