There is a code written in Windows Forms:

DateTime TimeToWorkServerTemp; DateTime ServerTemp = DateTime.Now; TimeToWorkServerTemp = new DateTime(ServerTemp.Year, ServerTemp.Month, ServerTemp.Day, ServerTemp.Hour, ServerTemp.Minute, 0); var ServtempDel = new System.Windows.Forms.Timer { Interval = 1000, Enabled = true }; ServtempDel.Tick += delegate { if (TimeToWorkServerTemp < DateTime.Now) { что то делается TimeToWorkServerTemp = TimeToWorkServerTemp.AddMinutes(1); ServtempDel.Enabled = true; } }; 

The problem is that in the Windows Form it quietly works, and if you use the code in the console, then the code does not work, tell me the reason?

  • WF, WPF, etc. create a context in which the timer events are processed. In the console application, you yourself must create a context. Faced with such a problem. Unfortunately, there are no examples left, but you can find information on this subject on the Internet freely. A similar question - Dmitry Chistik
  • ru.stackoverflow.com/a/535729/198316 study, this is just your console case - rdorn

1 answer 1

The reason is that this is System.Windows. Forms .Timer

Use System.Threading.Timer in console applications, and in WinForms, too. Or System.Timers.Timer , as suggested in the comments.

  • four
    Attention! System.Threading.Timer runs in another thread. see cross-thread programming lock , Mutex , etc. Correct if I'm wrong. - Dmitry Chistik
  • one
    And even better - System. Timers .Timer. - Pavel Mayorov