Good afternoon) There is a console application that takes data from a SQL table. We start application, and we should add one more record to datatable . How can I automatically update datatable periodically?

Created Thread , and set the timer

 static void Main(string[] args) { Thread t1 = new Thread(one); t1.Start(); TimerCallback time1 = new TimerCallback(one); Timer timeCB = new Timer(time1, null, 0, 300000); } 

Here I will write what the timer does. I do not know what to write. Tell me the code, please!

  public static void one(object state) { } 
  • And cron does not fit? - Hardc0re
  • @ M.Ivashchuk unfortunately not ( - propro17
  • What does it mean periodically in your case: every time you start the program? Or does the program work in the background and add data at a certain interval? What should be the result? - rdorn
  • @rdorn program should work in the background - propro17
  • one
    What timers do not fit? - vitidev

1 answer 1

In .NET, four standard timer classes are defined, each with its own characteristics. In your case, as correctly noted in the @vitidev comments, either System.Threading.Timer or System.Timers.Timer will do . On EnSO I found a link to a comparative article about timers: the answer to EnSO is an article in English (for non-English speakers, there is a comparative table of timers at the very end of the article). Also, the comparison of timers and their main purpose are given in the notes to the descriptions of classes, but there this description is rather modest.

I used System.Timers.Timer . The minimum usage example in the code below.

 static void Main(string[] args) { using (Timer timer = new Timer()) { timer.Interval = 2000;//интервал задается в миллисекундах timer.Elapsed += timer_Elapsed; timer.Start(); //Ставим основной поток на ожидание, т.к. таймер исполняется в //отдельном потоке и не препятствует завершению основного потока. Console.ReadLine(); } } static void timer_Elapsed(object sender, ElapsedEventArgs e) { //тут пишем код, который должен выполняться по событию таймера. //в вашем случае это будет вызов метода записи в таблицу на сервере, //либо собственно код этого метода. Console.WriteLine(e.SignalTime); } 

More detailed examples for both types of timers are contained in the class descriptions by reference at the beginning of the answer.

Of course, you can not just put the thread on hold, but perform other useful actions in the main thread while the timer is doing its job.

There is another solution to the problem. I can use a cycle with an exit by the counter or by pressing a key and Thread.Sleep to stop the main and only thread for a specified time interval, but this solution, despite its simplicity, does not seem to me a good one. When timer tasks are performed in separate threads, we can display additional information about the progress of tasks in the console, control the timer state and perform other actions. In the case of a single stream with pauses, we are deprived of freedom of maneuver; for the time of freezing the stream, the program simply stops responding to external stimuli and can only be closed forcibly.

  • thanks for the answer! in timer_Elapsed what to write? I do not create the record by hand. in sql server entry is added / changed / deleted. I just want to update periodically datatable . - propro17
  • the record changes in the server, and I only have to do the datatable updates - propro17