0

What is the best way to allow the following to be in a loop for 1 hour and have a 5 minute sleep? I have tried one loop which used stopwatch but that caused an error when trying to run it in debug.

string message = txtmessage.Text;
Clipboard.SetText(message);
SendKeys.SendWait("x");
SendKeys.SendWait("^v");
SendKeys.SendWait("{ENTER}");
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Connor Nee
  • 95
  • 9
  • Perhaps a timer? Note that this type of code is *extremely* fragile and probably not the correct way to do whatever it is that you're trying to do. What is it that you're trying to do? – Cody Gray - on strike Jun 18 '17 at 14:54
  • StopWatch didnt work because it is a diagnostic measure. You could use a DateTime object that will check the TimeSpan to see if 1 hour has ellapsed. The 5 minute sleep it will be for every 1 hour or in between ? – Nick Polyderopoulos Jun 18 '17 at 14:56
  • I need the program to do some input a user would do. When in-game it will press 'x' which brings up the chat box and then paste a message into it which a user puts into the text box 'txtmessage' and hits enter. I have found out that it does it more than once which I can't seem to find a solution on google. – Connor Nee Jun 18 '17 at 14:57
  • I had a look at datetime and it just states about using days instead of hours and minutes, could you provide me with a piece of code? – Connor Nee Jun 18 '17 at 15:01
  • @ConnorNee Take a look at [here](https://www.google.com/search?q=timer+in+C%23&rlz=1C1CHWL_enIR739IR739&oq=timer+in+C%23&aqs=chrome..69i57j0l5.3006j0j1&sourceid=chrome&ie=UTF-8) – Green Falcon Jun 18 '17 at 15:06
  • I currently have this now: https://pastebin.com/n3ZbDkHa and get this error: https://gyazo.com/ab1f006417886116cc9bbbd08e47a42e – Connor Nee Jun 18 '17 at 15:09
  • There is no shortage of questions on Stack Overflow already discussing how to perform some arbitrary operation on some periodic basis. See marked duplicates for three of the highest-voted examples. There's not enough context in your question to know what the best option for you would be. Something like `System.Threading.Timer` would work well for a console program. In a GUI program, you could use `System.Windows.Forms.Timer` (Winforms) or `System.Windows.Threading.DispatcherTimer` (WPF), but modern C# has `async`/`await`, which you can use with `Task.Delay()` for this. – Peter Duniho Jun 18 '17 at 18:36

1 Answers1

1

StopWatch didnt work because it is a diagnostic measure. You could use a DateTime object that will check the TimeSpan to see if 1 hour has ellapsed. The 5 minute sleep it will be for every 1 hour or in between ?

You can do it like this:

var dateTime = DateTime.Now.AddMinutes(1);
Console.WriteLine($"It will stop at: {dateTime}");
var now = DateTime.Now;
while (dateTime.CompareTo(now) > 0)
{
   //Do Job
   now = DateTime.Now;
}

Note that this is the simplest way i can think of. You can change it and make it better.

EDIT: This version of code uses Thread sleep and Timer to do the job you want. Be very careful when you calling the Thread.Sleep though because you can freeze your app if it put to sleep the ui thread of your app.

 Console.WriteLine($"Started at: {DateTime.Now}");
 Timer timer = new Timer(TimeSpan.FromSeconds(5).TotalMilliseconds) 
 {AutoReset = true};
 timer.Elapsed += (sender, args) =>
        {
            //Your code here.
        };
 timer.Start();

 Thread.Sleep(TimeSpan.FromMinutes(1));

 timer.Stop();
  • the loop will run for 1 hour but the code should only be executed every 5 minutes within that 1 hour. – Connor Nee Jun 18 '17 at 15:23
  • Anyone know what this error is for? https://pastebin.com/84T3Z6UM Referring to this: https://pastebin.com/BKPftni9 – Connor Nee Jun 18 '17 at 15:36
  • Your OnTimedEvent does not match the signature i think. By the way check my newest edit. I utilised what @Media said about the timer. – Nick Polyderopoulos Jun 18 '17 at 15:39
  • What does it do btw? also I get this erro using it https://gyazo.com/2a9f367757be5d729aaf5cf2a3c6eaaf – Connor Nee Jun 18 '17 at 15:42
  • The timer will launch an event every 5 seconds in my case, it will reset every time it reaches its limit. Then i use the thread.Sleep and after x minutes i stop the timer. (I could do some clean up to at this point.) EDIT: For your error link can you provide the code that threw the exception ? – Nick Polyderopoulos Jun 18 '17 at 15:45
  • I don't know how to find the code that threw it – Connor Nee Jun 18 '17 at 15:48
  • You could use the stacktrace to check the path of method calls that threw the exception – Nick Polyderopoulos Jun 18 '17 at 15:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/147005/discussion-between-connor-nee-and-nick-polideropoulos). – Connor Nee Jun 18 '17 at 15:53