c# - UWP - Headless app stops after 3 or 4 hours -
i have headless uwp app supposed data sensor every 10 minutes , send cloud. when run code headless app on raspberry pi stops measuring after 3 or 4 hours, no error (i have lot of logs). 3 or 4 hours. if start app @ 8, @ 11 or 12 stops...
it looks stopped because have cancellation token in place worked in tests, here not firing anymore. on app manager in device portal appears app running.
i noticed in performance page in device portal memory goes down 8 mb during measurements.
the strange thing ran same code headed app on rpi , on laptop , went well. worked continuously on 16 hours until stopped it. on both laptop , rpi there no memory issue, app used same amount of ram on whole period.
what cause behavior when running headless app?
here how call code headless app:
backgroundtaskdeferral deferral; private isettingsreader settings; private ilogger logger; private iflowmanager<palmsensemeasurement> flow; private iserviceprovider services; ibackgroundtaskinstance mytaskinstance; public async void run(ibackgroundtaskinstance taskinstance) { taskinstance.canceled += taskinstance_canceled; deferral = taskinstance.getdeferral(); mytaskinstance = taskinstance; try { setproperties(); var flowtask = flow.runflowasync(); await flowtask; } catch (exception ex) { logger.logcritical("#####---->exception occured in startuptask (run): {0}", ex.tostring()); } } private void setproperties() { services = sensorhubcontainer.services; settings = services.getservice<isettingsreader>(); flow = services.getservice<iflowmanager<palmsensemeasurement>>(); logger = services.getservice<ilogger<startuptask>>(); } private void taskinstance_canceled(ibackgroundtaskinstance sender, backgroundtaskcancellationreason reason) { logger.logdebug("startuptask.taskinstance_canceled() - {0}", reason.tostring()); deferral.complete(); }
and here how call code headed app:
private async task getmeasurementsasync() { try { flow = services.getservice<iflowmanager<palmsensemeasurement>>(); await flow.runflowasync(); } catch (exception ex) { measurements.add(new measurementresult() { errormessage = ex.message }); } }
the runflowasync method looks this:
public async task runflowasync() { var loopinterval = settings.noofsecondsforloopinterval; while (true) { try { logger.loginformation("starting new loop in {0} seconds...", loopinterval); //check previous unsent files await resender.tryresendmeasuresasync(); await task.delay(timespan.fromseconds(loopinterval)); await domeasureandsend(); logger.loginformation("loop finished"); } catch (exception ex) { logger.logerror("error in flow<{0}>! error {1}", typeof(t).fullname, ex); #if debug debug.writeline(ex.tostring()); #endif } } }
the problem 3rd party library had use , had called differently headless app. internally creating own taskscheduler if synchronizationcontext.current null.
Comments
Post a Comment