-1

I want to enter in an time where the next part of a program executes (Console Application). Something similar like this:

Console.ReadLine();

But with a timer that executes at a given time. As an example i wanna run the code at 10:00:00 (GMT), so i type in that time and at 10:00:00 it runs the next part of the code. Like a stop in the code till that time comes. I know im bad at explaining since im not english but im trying. Thank you!

yariza
  • 7
  • 2
  • If you're reading this from the `Console` what's to stop a user from closing the application? Thus preventing the code from executing at the given time? – Ryan Wilson Aug 24 '20 at 19:28
  • I think it is also dependent on the operating system this is intended for. Is it Windows only? Do you want it to be platform independent? – Hawk Aug 24 '20 at 19:29
  • Its hard to explain for me, i wanna make an input where you can enter in a time, as an example 10:00:00. At that time (the console stays open) the program continues to work. – yariza Aug 24 '20 at 19:30
  • Im working on Windows only currently. – yariza Aug 24 '20 at 19:30
  • Could you just parse the input as a `DateTime` and start a new thread that just waits for that time to arrive and then does something? Perhaps an example of what you mean would help. – Rufus L Aug 24 '20 at 19:31
  • Yeah that would work i guess, but i dont know how to do it. – yariza Aug 24 '20 at 19:57

3 Answers3

0

As a very basic solution, you could just loop with a wait until the desired time

var input = Console.ReadLine();
var time = TimeSpan.Parse(input);
var waitTill = DateTime.Now.Date + time;
var stopEvent = new ManualResetEvent();
while (DateTime.Now <= time && !stopEvent.WaitOne(30000){
    // Just wait
}
// Do next stuff

Note this is probably a bad idea though, and you should use TaskScheduler or something to handle this for you.

Milney
  • 6,253
  • 2
  • 19
  • 33
0

One idea is to parse the input as a DateTime and then start a new thread that just waits for that time to arrive and then does something. This way your program stays active, but the other code runs at the correct time.

For example:

var dummyText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + 
    "Vivamus vitae velit venenatis, malesuada sapien non, ultricies risus. " + 
    "Quisque magna risus, pulvinar in semper sit amet, tempus suscipit diam. " +
    "Cras eget posuere sem, maximus vestibulum tortor. Etiam sodales sit amet " +
    "eros non dapibus. Cras pellentesque eleifend risus, quis placerat nulla " +
    "vulputate a. Duis erat erat, congue sed justo at, hendrerit varius nunc. ";

Console.Write("What time should the text turn red: ");
var timeInput = DateTime.Parse(Console.ReadLine());  // Enter something like "12:15 PM"

// Start a background task that waits for the specified time to arrive
Task.Factory.StartNew(() =>
{
    while (DateTime.Now < timeInput) Thread.Sleep(1000);  // Check time every second
    Console.ForegroundColor = ConsoleColor.Red;           // Change text color
});

// Endless loop that writes out one character of 
// text at a time so we can see when it turns red
for(int i = 0; ; i++)
{
    if (i == dummyText.Length) i = 0;
    Console.Write(dummyText[i]);
    Thread.Sleep(150);
}

The above was based on my understanding that you wanted the program to remain actively doing something while waiting for the time. If you just want the whole thing to wait, then you don't need an additional task. All you need is a single line that sleeps until the elapsed time has arrived:

// Pause code execution until the specified time arrives
while (DateTime.Now < timeInput) Thread.Sleep(100);
Rufus L
  • 36,127
  • 5
  • 30
  • 43