Windows 8 WinRT background task time resolution - windows-runtime

Can a WinRT background task triggered by a 15 min periodic timer start it's own thread, which then sleeps and wakes up every say 30 seconds? I.e. if you need a background task to do stuff more often than the minimum resolution, would this hack work? If battery life is not a concern...and would it would even when not on AC?

The background task can spawn a thread; BUT it may only use up to two CPU seconds per 15 minutes. In other words, you don't get permission to just run whatever thread work you'd like, you need to keep within constraints. The details are explained in the Introduction to Background Tasks document MS published:
An app on the lock screen receives a certain amount of CPU time at
regular intervals for its background tasks. If the app uses all of its
available CPU time, its background tasks are suspended until the app’s
CPU quota is replenished at the next generation for CPU quota updates.

Related

Why does dottrace significantly improves startup time of my web application?

I am trying to find out why my web application (.NET + Sitecore) takes so long to start, but dottrace makes it start faster.
The current startup time is about 270 seconds, but when I start application with dottrace it actually takes 30 seconds. This is reproducible every time including other developers machines.
I am using .NET 4 and IIS 10 (app pool runs as Network Service).
The performance depends on the Windows Kernel Timer resolution, and dotTrace changes the value of this timer to the minimum possible value. So if dotTrace makes your program faster, it may indicate that (for example) there are many Thread.Sleep(n) calls in your program, where "n" is a relatively small number (1-5-10-20 milliseconds). Do you have such calls in your application?

How to increase battery efficiency for a voice recording application in Windows Phone 8?

I have developed a voice recording app using WasApi for Windows Phone 8. But users are facing battery problem a lot and also the screen is not getting timeout while the recording is on.
And if users press the lock button on background recording is getting paused. Can anyone tell me how to solve these issues?
I am unaware of a way to turn off the screen while recording, or of a way to record while the application is in the background. That does not mean it's not possible, only that I don't know how. It may not be possible now, but become possible in the future. Other answers may explain how to do this.
So I'll list ways to reduce battery consumption while your application is running in the foreground and the screen is on:
Black display. Bright images require a lot more power than dark ones. Depending on the display technology, black pixels require a lot less power than dark pixels. Look at the Lumia Glance feature which can be always on and still requires days to drain the battery.
No animations. Depending on the display technology, redrawing the screen may require more power. In any case, calculating the animation to be drawn on the screen prevents the CPU from sleeping. Having an animation that only updates every second instead of every 15 milliseconds should already be a big improvement.
No wait loops/busy wait. If the CPU needs to wait for something don't use this pattern:
while (true)
{
if (arewethereyet())
break;
}
Cluster work into batches. The CPU needs to be able to sleep and ideally it needs to be able to sleep for long continuous periods of time. Use a long buffer duration for the microphone and don't fetch the buffer too aggressively.

My Periodic Task just does not work

While attached to debugger it runs just fine. The Periodic Task is invoked and runs over and over, but when I deploy it to my device It seems to run 1-2 times and then stops.
What It does is setting the live tile background image from isolated storage. The images are created in the application and then saved to isolated storage. As mentioned it works well while attached to the debugger.
The only constraint I could think that could break it would be the memory cap. The application creates and saves 40 images of ~25kB each, and that isn't 1 MB! The application is maybe <4 MB, so that is 5 MB... a lot less than the 11 MB minimal requirement.
So it can't be the memory cap kicking in. Two consecutive unhandled crashes should also break the task, but I've thrown all the code in the task's OnInvoke() in a try/catch.
Now I'm out of ideas what stopping my periodic task when running without being connected to visual studio running in debugger. Any clues?
Firstly are you using Windows 8.1 phone by any chance? Since there is an issue with Periodic tasks do not run on windows phone 8.1 devices as you can see on this forum
Background agent can’t use more than 6MB of memory. You can get the current memory usage using the following snippet :
var memory = DeviceStatus.ApplicationMemoryUsageLimit
- DeviceStatus.ApplicationCurrentMemoryUsage;
automatically executed by the OS each 30 minutes
the operation can’t exceed 25 seconds per run
if the phone switch to battery saver mode the background agent may not be executed
on some devices only 6 background agents may be planned simultaneously
agents can’t use more that 6MB of memory
agents have to be re-planned each 2 weeks
an agent that crashes two times is automatically disabled by the system
Periodic tasks are unscheduled after two consecutive crashes. You need to make sure that this doesn't happen (check internet connectivity if required, set a timeout on web requests, etc.).
You should place your code in a try/catch block and log exceptions in the Isolated Storage to see what happened afterwards.
Here is the list of constraints that apply on scheduled agents (MSDN): Constraints for all Scheduled Task Types
Here is also a series of blog posts that could help you: Windows Phone: Background Agents Pitfalls
Have you actually measured and logged the memory that's being used? What you're saying isn't very correct:
When the background agent starts it has already taken 5-6MB to load what it needs from the .NET framework.
If you mean that the compressed files are 25KB each, you should know that the images in the memory are not compressed (at least not that much).
There are two things you can try:
Use this property and check the peak memory usage: DeviceStatus.ApplicationPeakMemoryUsage. Write it to some file (maybe every 5 images or so) and check if it's okay. Paste the results, please.
Note: When testing the memory usage, it's best to build the app in "Release" and run it without debugging on a device. That's most accurate. There are some minor variations, so you should run the agent several times to be sure it's working within the limits. You can force start it from the app using ScheduledActionService.LaunchForTest.
Also, I'd suggest you subscribe to the Application.Current.UnhandledException event and mark all exceptions as handled (and log them, so that you can fix them). That's for extra safety.
P.S. When the background agent stops executing, is it "blocked" in the list of background tasks on the device?

Metro App BackgroundTask TimeTrigger/MaintenanceTrigger Usage

I read an article on BackgroundTasks: TimeTrigger and MaintenaceTrigger.
Here they demonstrate how these triggers can be used to download email. I'm trying to understand the practicality and appropriateness of this approach.
Quotas for BackgroundTasks on LockScreen are 2 seconds CPU time and non-LockScreen is 1 second CPU time.
Given this restriction, how is it possible that one can download emails in this amount of time? Surely, just establishing a connection to the remote server will take more time than that?
Am i misunderstanding something about how BackgroundTasks work or is this article inaccurate?
http://blogs.msdn.com/b/windowsappdev/archive/2012/05/24/being-productive-in-the-background-background-tasks.aspx
CPU Time is not the same as the amount of seconds that have passed. Your link references a Word Document, Introduction to Background Tasks, which contains the following:
CPU usage time refers to the amount of CPU time used by the app and not the wall clock time of the background task. For example, if the background task is waiting in its code for the remote server to respond, and it is not actually using the CPU, then the wait time is not counted against the CPU quota because the background task is not using the CPU.
If you are establishing a connection to the mail server (and waiting for it to respond), then you are not using any CPU. This means the time that you spent waiting is not counted against you.
Of course, you will want to test your background task to make sure that it stays within the limits.

Windows Phone 8 - task on alarm

I've written a code that reads qr/barcode on demand. However I need background agent to run this piece of code/app while system alarm rings. I haven't been able to think how to do it.
You can't launch the program from background agent. You can update Live Tiles or show Shell toast or do little background task from background agent.
Windows phone 8 have two types agent- 1) PeriodicTask & 2)ResourceIntensiveTask
PeriodicTask: Periodic agents run for a small amount of time on a regular recurring interval.Periodic agents typically run every 30 minutes. To optimize battery life, periodic agents may be run in alignment with other background processes and therefore the execution time may drift by up to 10 minutes.Periodic agents typically run for 25 seconds. There are other constraints that may cause an agent to be terminated early.
ResourceIntensiveTask: Resource-intensive agents run for a relatively long period of time when the phone meets a set of requirements relating to processor activity, power source, and network connection. Resource-intensive agents typically run for 10 minutes. There are other constraints that may cause an agent to be terminated early.Resource-intensive agents do not run unless the device has a network connection over Wi-Fi or through a connection to a PC.Resource-intensive agents do not run unless the device’s battery power is greater than 90%.
For more please read Agent