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();