i building rss reader , want add periodic task check new feed items. if finds new items, update live tile of app accordingly.
the problem encounter using downloadstringasync() method download feeds , check if contain new items. so, downloading process may take longer 20 seconds (the time periodic task given in order complete actions).
all want ensure notifycomplete() method called before agent gets terminated os, after 20 seconds of action. reason, want register dispatcher timer interval of 15 seconds, call notifycomplete() method in tick event.
however, tried declare , use dispatcher timer, , invalid cross-thread access error raised. periodic task code includes following:
public class scheduledagent : scheduledtaskagent { //register dispatchertimer dispatchertimer mastertimer = new dispatchertimer(); private static volatile bool _classinitialized; public scheduledagent() { if (!_classinitialized) { _classinitialized = true; // subscribe managed exception handler deployment.current.dispatcher.begininvoke(delegate { application.current.unhandledexception += scheduledagent_unhandledexception; }); } //set timer properties mastertimer.interval = timespan.fromseconds(15); mastertimer.tick += mastertimer_tick; } protected override void oninvoke(scheduledtask task) { //todo: add code perform task in background mastertimer.start(); //call downloadstringasync() , perform other tasks... //call notifycomplete() after download complete. // } private void mastertimer_tick(object sender, eventargs e) { mastertimer.stop(); //there no more time left, must call notifycomplete() avoid //having periodic task terminated os notifycomplete(); } } the question is, why happening , how can resolve issue. thank in advance!
cross-thread access error raised when try chage ui property thread other 1 created control.
to pass use invoke method exist in control
the code should this:
control.invoke((methodinvoker)delegate{ //do work here example notifycomplete(); });
Comments
Post a Comment