I am new to C # you couldn’t help me fix the code. That the action would be repeated every second. Ie got out the message box every second.

void StartTimer() { int timeout = 0; while (true) { timeout = (1 - DateTime.Now.Second) * 1 - DateTime.Now.Millisecond; Thread.Sleep(timeout); MessageBox.Show("Nen"); } } 
  • Calling MessageBox.Show("...") stops the current execution of the code (current thread) until the user closes it. Therefore, your code will not work. To implement your plans, you need to use multithreading or a timer that does it for you. Therefore, I advise you to postpone your plans until you get to these topics. - Alex Krass
  • It is better to start learning from console applications, as there are fewer problems. - iluxa1810

1 answer 1

 //Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Timers; using System.Threading.Tasks; namespace Rextester { public class Program { private static System.Timers.Timer aTimer; public static void Main(string[] args) { SetTimer(); Console.ReadLine(); aTimer.Stop(); aTimer.Dispose(); } private static void SetTimer() { // Таймер с интервалом в секунду aTimer = new System.Timers.Timer(1000); // Событие когда таймер идет aTimer.Elapsed += OnTimedEvent; aTimer.AutoReset = true; aTimer.Enabled = true; } private static void OnTimedEvent(Object source, ElapsedEventArgs e) { Console.WriteLine(e.SignalTime); //TODO } } } 

you can check here

  • It gives an error when I added it to my code. In this line public static void Main (string [] args) - Andrey Reyz
  • Error CS0017 The program has multiple entry points. Compiling with / main will allow you to specify the type containing the entry point. Themoon C: \ Users \ Themoon \ Themoon \ Form1.cs 55 N / A - Andrey Reyz
  • @AndreyReyz add the functions SetTimer() and OnTimedEvent(Object source, ElapsedEventArgs e) to your code. Then in OnTimedEvent write down what you need to perform for a second. And call the timer using SetTimer(); where you need ... for example in Form_Load() - Vitali Shebanits
  • change your example then. - Andrei Reyz
  • @Andrey Reyz what is wrong with my example ?? you are trying to cram Main into WinForm .... in my opinion I’m not guilty here - Vitaly Shebanits