4

i want to truncate table using console application with parameter hour. for example, i want to run query truncate at 12.00 AM using time in system.

this is my code in console application using c#.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {            
            string getHour = DateTime.Now.ToString("h:mm:ss tt");

            if (getHour == "12:00:00 AM")
            {
                Console.WriteLine("Do action to run query truncate");
                //in this line i will execute query truncate.
            }        

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        } 
    }
}

And this is not working. please give me solution or sample code to fix my problem. thanks...

AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38

4 Answers4

1

When you run your program, it looks on the clock quickly, and if it is not exactly midnight immediately exits. (To be more precise it prints some message and waits until keypress). I believe you wish to wait until midnight. If the exact timing is not that important (I mean some seconds early or late is acceptable), the simplest solution is:

static void Main(string[] args)
{
    Thread.Sleep(DateTime.Today.AddDays(1) - DateTime.Now);
    Console.WriteLine("It's about midnight, I go to sleep");
}
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
1

I feel like the issue is with the flow of execution of your code. You're calling the code once and then it stops. It checks once NOW and get the current time. If the time is 12:00:00 AM, you can pass into the if statement, but you'll need to literally run this at 12:00:00 System time on the dot, which is nearly impossible.

You should consider a Windows Service or using Windows Task Manager: C# Console Application - Keep it running.

Community
  • 1
  • 1
Alex G
  • 24
  • 3
1

As mentioned in the other answers you shouldn't be using your app to schedule the task. It looks like you are doing a database maintenance task so the first thing I would look at is...

1) See if your database can schedule tasks. SQL Server Agent for example can schedule stored procedures (or adhoc SQL) to be run at set times and would accomplish your task if you are using that for your database. More info here.

If your database can't do this or you want to do other things other than truncate the table then...

2) try using Windows Task Scheduler

This can launch an application at set times, is easy to setup and then your application can just do the job it's mean to do (e.g. truncate the table) and not worry about the scheduling.

Community
  • 1
  • 1
Quantumplate
  • 1,104
  • 8
  • 15
1

I would suggest few things on your code:

  1. Don't use string comparison, but uses DateTime directly (take a look on TimeOfDay.TotalSeconds). This way, it makes the comparison a lot easier
  2. Use repeated calls instead of just one call (unless you can really be sure that you run your program exactly at 12). This way you make your program work for you more.
  3. To make it more robust, provide some ways to give tolerance to your system. This is how good automation should be.

Example:

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {

            while (true) { //read 2. assuming this is to be run forever in this example, but gives also some way to break the loop whenever necessary in your app
                if (DateTime.Now.TimeOfDay.TotalSeconds <= 1) { //read 1. and 3. this way, you give tolerance of 1 second. Your action will be run on 12:00:00 - 12:00:01
                    Console.WriteLine("Do action to run query truncate");
                    //  //in this line i will execute query truncate.
                    break; //break after saving once, for instance, and run again when the time close... This is to prevent possible multiple executions...
                }
                System.Threading.Thread.Sleep(500); //read 2. and 3. check every 500 millisecond, at least to give chance to check the time twice per second
            }

            // Keep the console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
Ian
  • 30,182
  • 19
  • 69
  • 107
  • Hey Lan, from your sample code, "DateTime.Now.TimeOfDay.TotalSeconds" this for totalseconds from a day. how if i want set at 8.15 AM ? Thanks Lan – developer satu Dec 26 '15 at 14:43
  • My name is Ian (It is an "i", the 9-th alphabet), btw. You can use other alternatives like `TotalMinutes` and `TotalHours` too.. you can also use `Minute` and `Hour` and `Second` (as not to calculate the total) even `Millisecond`. All are available in the `DateTime` class. It is really handy. ;) so if I were you, to set at 8:15, I will use DateTime.Now.Hour == 8 and DateTime.Now.Minute == 15 and DateTime.Now.Second <= 1 – Ian Dec 26 '15 at 14:49
  • You can also use `DateTime.Now.TimeOfDay` `Hours`. If you know that it is going to be 8.15, then you know that `DateTime.Now.TimeOfDay.Hours >= 8.25` (note that the .25 is fraction of hour, that is 15 minutes) combined with DateTime.Now.Second <= 1 the key is using `DateTime` class. When you explore it, you will get to know its benefit. ;) Just a side question: "Satu" is one in Malay/Indonesian language, right? Are you a Malaysian or an Indonesian? – Ian Dec 26 '15 at 14:55
  • Thanks ian... it work. yes im from indonesian ian. nice to meet you ian :) – developer satu Dec 26 '15 at 15:22
  • Great! Glad that you find it working. ;) I see.. no wonder.. In that case, Selamat Hari Natal! = Merry Christmas! – Ian Dec 26 '15 at 15:26