xamarin.android - Android Scheduled Timer - Mono for Android or Java -
i need run method every 5 seconds using mono android. there scheduled timer in android? have tried code, however, fails start:
using system; using system.collections.generic; using system.linq; using system.text; using android.app; using android.content; using android.os; using android.runtime; using android.views; using android.widget; using glass.core.interfaces; using glass.core; using java.util; namespace glass.ui.an { [application(label = "glass", icon = "@drawable/icon")] public class glassapplication : application { context context; public glassapplication (intptr handle, jnihandleownership transfer) : base(handle, transfer) { this.context = basecontext; } public override void oncreate () { base.oncreate (); timer timer = new timer (); timer.scheduleatfixedrate (new customtimertask(context), new date(datetime.now.year, datetime.now.month, datetime.now.day, datetime.now.hour, datetime.now.minute), 5000); } } public class customtimertask: timertask { context context; public customtimertask(context context) { this.context = context; } public override void run () { glasswebserviceprovider p = new glasswebserviceprovider (context); p.sendcardreaders (); } }
}
why not use system.timers.timer
?
var timer = new timer(); //what when time elapses timer.elapsed += (sender, args) => firethemissiles(); //how (5 sec) timer.interval = 5000; //start it! timer.enabled = true; private void firethemissiles() { //but i'm le tired... }
another approach make new task
or thread
, let sleep 5 seconds every time:
task.factory.startnew(() => { while (true) { // stuff thread.sleep(timespan.fromseconds(5)); } }); threadpool.queueuserworkitem(thread => { while (true) { // stuff thread.sleep(timespan.fromseconds(5)); } });
Comments
Post a Comment