M
Markus Freitag
Guest
Hello!
I have the following requirement, every 60 seconds I have to do something.
I solved it with thread and used the property Totalseconds.
With seconds it is not working, 58, 59, 0, 1, 2 and so one.
The question, can I solve it this way or is there a better solution?
- With Task.Run ?
- How can I solve this with a break ?
Supend is old.
Maybe you can publish 2, 3 examples (with thread and task)?
And the advantages and disadvantages.
Thank you in advance.
With best regards Markus
protected Thread ThreadCycleDataUpload;
protected ManualResetEvent ThreadCancelDataUpload;
ThreadCancelDataUpload = new ManualResetEvent(false);
ThreadCycleDataUpload = new Thread(CycleDataUpload);
protected void CycleDataUpload()
{
Stopwatch time = new Stopwatch();
time.Start();
while (!ThreadCancelDataUpload.WaitOne(0))
{
// Trace.WriteLine($"---A--- {time.Elapsed.Seconds} {time.Elapsed.TotalSeconds}");
if (time.Elapsed.TotalSeconds >= 60 )
{
RequestDataUpload();
time.Reset();
time.Start();
}
Thread.Sleep(1000);
}
Continue reading...
I have the following requirement, every 60 seconds I have to do something.
I solved it with thread and used the property Totalseconds.
With seconds it is not working, 58, 59, 0, 1, 2 and so one.
The question, can I solve it this way or is there a better solution?
- With Task.Run ?
- How can I solve this with a break ?
Supend is old.
Maybe you can publish 2, 3 examples (with thread and task)?
And the advantages and disadvantages.
Thank you in advance.
With best regards Markus
protected Thread ThreadCycleDataUpload;
protected ManualResetEvent ThreadCancelDataUpload;
ThreadCancelDataUpload = new ManualResetEvent(false);
ThreadCycleDataUpload = new Thread(CycleDataUpload);
protected void CycleDataUpload()
{
Stopwatch time = new Stopwatch();
time.Start();
while (!ThreadCancelDataUpload.WaitOne(0))
{
// Trace.WriteLine($"---A--- {time.Elapsed.Seconds} {time.Elapsed.TotalSeconds}");
if (time.Elapsed.TotalSeconds >= 60 )
{
RequestDataUpload();
time.Reset();
time.Start();
}
Thread.Sleep(1000);
}
Continue reading...