Reputation: 4656
So I have a Timer
in my Activity
and I want to run that every 10 seconds.
I created a System.Threading.Timer
:
timer = new Timer ((o) => {
Action syncAct = new Action (async delegate() {
await FetchData();
});
RunOnUiThread (syncAct);
}, null, 0, 10000);
The problem here is that await FetchData()
takes longer than 10 seconds and that causes the timer to go on forever. I need the timer to start every time AFTER the sync completes. How can I do that?
Thank you for your time.
Upvotes: 0
Views: 102
Reputation: 3568
Try spawning a thread that sleeps for 10 seconds and runs the code.
Roughly like so:
\\declare doRun as a boolean field
new Thread()
{
public void run()
{
yourClass.doRun = true;
while(yourClass.doRun)
{
fetchData();
try {
sleep(10000);
} catch(InterruptedException e)
break;
}
}
}
}.start();
Set doRun
to false
to exit the thread.
Or you could assign the thread to a variable instead of an anonymous thread, and call yourThread.interrupt();
Upvotes: 1