Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
Use CancellationToken and CancellationTokenSource with the CancelAfter
method
But it would be better if you could describe the task more specifically, otherwise it looks like an XY problem .
https://docs.microsoft.com/en-us/dotnet/api/system...
https://metanit.com/sharp/tutorial/12.5.php
Because You didn't say you're using TPL or writing in WinForms or WPF, so I'm assuming it's just plain synchronous code somewhere in the console.
Start a timer above your loop, check the time inside the loop and, upon reaching it, exit the loop through break. On the knee, without checks, but I think you will catch the meaning
var dateTimeStart = DateTime.Now;
const PERIOD = 3 * 60 * 1000;
while(true)
{
...
var dateTimeNow = DateTime.Now;
var interval = dateTimeNow - dateTimeStart;
if(interval.Milliseconds > PERIOD)
{
break;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question