static void Main(string[] args)
{
Console.WriteLine("Press any key to stop the timer...");
bool ThreadStopped = false;
Thread TimerThread = new Thread(delegate ()
{
DateTime StartingTime = DateTime.Now;
while (!ThreadStopped)
{
Console.Write($"{(DateTime.Now - StartingTime).TotalSeconds}\r");
Thread.Sleep(1000);
}
});
TimerThread.Start();
Console.ReadKey();
ThreadStopped = true;
TimerThread.Join();
}
Console.WriteLine("Press any key to stop the timer...");
bool ThreadStopped = false;
Thread TimerThread = new Thread(delegate ()
{
Console.OutputEncoding = Encoding.UTF8;
DateTime StartingTime = DateTime.Now;
bool MessageShown = false;
while (!ThreadStopped)
{
int SecondsSinceStart = (int)(DateTime.Now - StartingTime).TotalSeconds;
Console.Write($"{SecondsSinceStart}\r");
if (SecondsSinceStart >=100 && !MessageShown)
{
Console.WriteLine("\r\nтекст определенный");
MessageShown = true;
}
Thread.Sleep(500);
}
});
TimerThread.Start();
Console.ReadKey();
ThreadStopped = true;
TimerThread.Join();
я смог сделать только такое
var timer = new System.Timers.Timer(1000);
timer.Elapsed += OnTimerElapsed;
timer.Start();
Console.WriteLine("Press any key to stop the timer...");
Console.ReadKey();
timer.Stop();
}
private static void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
Console.WriteLine("Hello, world!");
}