1

I'm making an application that will notify the user every 1, 5 or 30 minute or even every hour. For example the user opens the program in 5:06 and the program will notify the user in 6:06.

So my current code is notifying the user every 5 minutes using Thread.Sleep() function, but I find it kinda lame.

This is my code:

public void timeIdentifier()
    {
        seiyu.SelectVoiceByHints(VoiceGender.Female);
        while(true)
        {
            string alarm = String.Format("Time check");
            seiyu.Speak(alarm);
            string sayTime = String.Format(DateTime.Now.ToString("h:mm tt"));
            seiyu.Speak(sayTime);
            // It will sleep for 5 minutes LOL
            Thread.Sleep(300000);
        }
    }
Přemysl Šťastný
  • 1,676
  • 2
  • 18
  • 39
Kurogami
  • 15
  • 2
  • 7
  • 1
    why dont you use Task Schedular for that.Just a suggestion – Vicky Jul 19 '16 at 09:21
  • Look at my answer here (is an implementation of a RecurrentCancellableTask), it may solve your problem easily: http://stackoverflow.com/questions/7472013/how-to-create-a-thread-task-with-a-continuous-loop/35308832#35308832 – Juan Jul 19 '16 at 09:22
  • 1
    I suggest using `Timer` – Dmitry Bychenko Jul 19 '16 at 09:22
  • @Vicky: how do you configure the task scheduler to notify someone if a program was opened for at least one minute? – Tim Schmelter Jul 19 '16 at 09:24
  • @TimSchmelter-Sir,i was of the opinion that advance setting panel under Triggers tab gives us option to configure windows schedular to any minute of our choice. – Vicky Jul 19 '16 at 09:28
  • the application i am making will go to the task tray if minimized..Notification Tray I suppose? – Kurogami Jul 19 '16 at 11:05

3 Answers3

5

You can use Timers instead of Thread.Sleep():

public class Program
{
    private static System.Timers.Timer aTimer;

    public static void Main()
    {
        aTimer = new System.Timers.Timer(5000); // interval in milliseconds (here - 5 seconds)

        aTimer.Elapsed += new ElapsedEventHandler(ElapsedHandler); // handler - what to do when 5 seconds elaps

        aTimer.Enabled = true;

        // If the timer is declared in a long-running method, use
        // KeepAlive to prevent garbage collection from occurring
        // before the method ends.
        //GC.KeepAlive(aTimer);
    }

    //handler
    private static void ElapsedHandler(object source, ElapsedEventArgs e)
    {
        string alarm = String.Format("Time check");
        seiyu.Speak(alarm);
        string sayTime = String.Format(DateTime.Now.ToString("h:mm tt"));
        seiyu.Speak(sayTime);
    }
}
Roman
  • 11,966
  • 10
  • 38
  • 47
0

If you like newer .NET TPL syntax yo can write it like this:

internal class Program
{
    private static void Main(string[] args)
    {
        Repeat(TimeSpan.FromSeconds(10));
        Console.ReadKey();
    }

    private static void Repeat(TimeSpan period)
    {
        Task.Delay(period)
            .ContinueWith(
                t =>
                {
                    //Do your staff here
                    Console.WriteLine($"Time:{DateTime.Now}");
                    Repeat(period);
                });
    }
}
George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
-1

You can use Timer object (System.Threading). The timer object has interval not a timeout.

    static void Main(string[] args)
    {
        int firstCallTimeOut = 0;
        int callInterval = 300000;
        object functionParam = new object();//optional can be null
        Timer timer = new Timer(foo,null,firstCallTimeOut,callInterval);
       
    }
    static void foo(object state)
    {
       //TODO
    }
  • 1
    The garbage collector will eventually remove the reference to a local object. So in general case the timer have to do a class field. – Alexander Petrov Jul 19 '16 at 10:06