Question

i wanted to create a program like this .
For every minute the time should be printed in the format of h:m .
For every 5 min it should print "break"
this should continue for 24 hours .. like this

0:0
0:1
0:2
0:3
0:4
break
0:6
0:7
0:8
0:9
break
0:11
.
.
.
23:59

i came with a program that solves it ..but i never used DateTime or any time function , i just used Thread.Sleep to dalay printing for 1 minute every time ... i wanted to use some other method other than Thread.Sleep to solve it ... so please guide me .. (sorry for my Bad english)


this is how i did with Thread.Sleep .
please provide me with any other solutions

using System;
using System.Threading;

class try22
{
    public static void Main()
    {
        for(int i=0;i<24;i++)
        {
            for(int j=0;j<60;j++)
            { 
                if(j%5 != 0 || (j == 0 && i == 0))
                {
                    Thread.Sleep(20);        
                    Console.WriteLine(i+":"+j);
                }
                else if(j%5 == 0 )
                {
                    Thread.Sleep(20);
                    Console.WriteLine("break");
                }
            }
        }
     }
}




thanks guys i came up with the solution of using actual dates instead of array numbers in my problem

im getting weird errors with timer .. :( so i used thread.sleep itslef

using System;
using System.Threading;

class try22
{
    public static void Main()
    { 
        DateTime dt1 = new DateTime();
        dt1 = DateTime.ParseExact("0:0", "H:m",null);
        int cford=dt1.Day+1;
        for (; dt1.Day!=cford; )
        {
           dt1 = addm(dt1);
           Console.WriteLine(dts(dt1));
           Thread.Sleep(60000);
        }
    }
    public static string dts(DateTime dt)
    {

        string tmp = dt.ToString("H:m");
        if (dt.Minute % 5 == 0)
            return "BREAK";
        else
            return tmp;
    }
    public static DateTime addm(DateTime dt)
    {
        return dt.AddMinutes(1);
    }
}
Was it helpful?

Solution

Which of these were you asked for?

  1. Show the current time once per minute
  2. Show the current time at the start of every minute like an alarm

Assuming 1, here's a couple of hints in the right direction (which should be helpful either way):

  1. You can get the current date and time as a DateTime object using DateTime.Now
  2. DateTime objects can return custom string output using .ToString("format").
  3. Format is specified with a custom date and time format string. For example, to get the current hour in 24-hour time (without leading zeroes) you could use DateTime.Now.ToString("H").
  4. As per the reference, you can include a string literal (unprocessed string) in your format. For example DateTime.Now.ToString("'Hour is: 'H") would return Hour is: 6
  5. You can get the "minute" value of a DateTime object as an int using .Minute. For example, int minute = DateTime.Now.Minute;
  6. If you want some code to run periodically, one way is to move it into its own method then setup a System.Threading.Timer like this:

    void SomeMethod(object state) { /* DO STUFF HERE */ }
    
    // Initialise the timer in your main() method 
    // As per MSDN for System.Threading.Timer, first number (0) is start delay.
    // Second number (60000) is interval in milliseconds (60 seconds)
    // This will cause SomeMethod to be called once every 60 seconds starting now.
    Timer timer = new Timer(new TimerCallback(SomeMethod), null, 0, 60000);
    
  7. You will need to stop your application exiting straight away after making the Timer (otherwise it will never get to run). One easy way to do this in a command line application is place a Console.Read() at the end of your Main() method which will wait for user input.

OTHER TIPS

I have used Timer instead of Thread

 class Program
    {
        private static System.Timers.Timer aTimer;
        static int j = 0;
        static int i = 0;
        public static void Main()
        {         
            // Create a timer with a Minute interval.
            aTimer = new System.Timers.Timer(60000);

            // Hook up the Elapsed event for the timer.
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

            // Set the Interval to 1 Minute (60000 milliseconds).
            aTimer.Interval = 60000;
            aTimer.Enabled = true;

            Console.WriteLine("Press the Enter key to exit the program.");
            Console.WriteLine(0 + ":" + 0);
            Console.ReadLine();
        }

        // Specify what you want to happen when the Elapsed event is 
        // raised.
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
        {            
            j++;
            if (j == 60)
            {
                Console.WriteLine("break");
                j = 1;
                i = i + 1;
            }
            if (i == 24)
            {
                i = 0;
            }

            if (j % 5 != 0 || (j == 0))
            {                              
                Console.WriteLine(i + ":" + j);
            }
            else if (j % 5 == 0)
            {
                Console.WriteLine("break");
            }
        }
    }

I am not sure weather you want to use actual System time to start with or just the time since program execution started. Solution i am posting uses time since program started.

using System;
using System.Collections.Generic;
using System.Text;
using System.Timers;
namespace ConsoleApplication1
{
   class Program
 {
    TimeSpan tt;
    static void Main(string[] args)
    {
        Program p = new Program();
        System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(p.run));
        t.Start();
        while (true) ;
    }

    void run()
    {
        tt=new TimeSpan(0,1,0);
        //Timer interval decides when even will be fired.
        Timer t = new Timer(60000);
        t.AutoReset = true;
        t.Elapsed += new ElapsedEventHandler(t_Elapsed);
        t.Start();

    }

    public void t_Elapsed(object sender, EventArgs e)
    {

        if (tt.Minutes % 5 == 0)
            Console.WriteLine("Break");
        Console.WriteLine(tt.Hours.ToString()+":"+tt.Minutes.ToString());
        tt = tt.Add(new TimeSpan(0, 1, 0));
    }
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top