Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I start a Windows App Background Task immediately after registering it?

I am writing a Metro App that will only run on PCs (so there is no, or at least less, worry about the battery life). I need it to register and run a background task when the user clicks a button. It registers fine, but I have found no way to make it run sooner than at a 15 minute delay, which is useless for this app. Any ideas how I can get it to run immediately?

Also I have found a similar question here that says its not possible, but someone must have figured out a way.

Edit:

My reasoning for requiring this task is such:

My app will be reading and writing files as they appear over an extended period of time, anywhere from a few minutes to several hours. During the majority this time, it is unlikely that the app will be presented in the foreground. Because I need the code to be able to continue executing, it seemed the best way was to create a background task, but I can find no way to manually initiate this task outside of Visual Studio, though in Visual Studio it works perfectly. If there is a more appropriate way to handle this execution, I am willing to use it.

like image 343
Garrett Avatar asked Jan 07 '13 13:01

Garrett


People also ask

How do I get a program to run in the background at startup?

Open Windows Explorer (you can use Win+E for this). Paste this path into the location bar: %appdata%\Microsoft\Windows\Start Menu\Programs\Startup . If you don't see your program there, try this location instead: %programdata%\Microsoft\Windows\Start Menu\Programs\Startup.

How do I force an app to run in the background windows?

Select Start , then select Settings > Privacy > Background apps. Under Background Apps, make sure Let apps run in the background is turned On. Under Choose which apps can run in the background, turn individual apps and services settings On or Off.

How to make background task in c#?

For a C# or C++ app, in your app project, right-click on References and select Add New Reference. Under Solution, select Projects and then select the name of your background task project and click Ok. To the background tasks project, add a new class that implements the IBackgroundTask interface.


1 Answers

EDIT

Revision due to the extra information from OP.

The only way I can think you could do this via a metro app is to ask your user to "snap" your app against the desktop - on your snapped app they can hit start/stop and carry only using the desktop at the same time with both apps running in the foreground.


I think my other answer is not complete so I will try to offer a better answer.

To clarify: you state that your whole reason for using a background task is so your app will continue running some code even if your app is suspended.

Background Tasks:

You cannot trigger a WinRT background task to run immediately.

Bear in mind that a background task by default has 1 second of CPU every 2 hours and there's no way of specifying when it will activate - you can only use opportunistic triggering - no good to you in this case.

Further, even if your app is made a lock-screen application, you cannot specify the immediate processing of a background task unless you are using push notifications, which aren't appropriate for what you are trying to do. For a lock-screen app you'd generally get 2 seconds of CPU every 15 minutes.

OnSuspending:

However, if your app is being suspended you still have a few seconds (5, according to this: http://www.redlist.ch/post/Time-limit-on-suspending-a-WinRT-app.aspx) of time to tidy up before your app is suspended by the OS. This suspension-grace-time is longer than the time you would have in the background task anyway!

Therefore, I would suggest not using background tasks and instead to initiate a background thread (as per my other answer) to do your button-press action (using local state to record its progress). Now, if you also hook into the App onsuspending event, you can use this to see if something hasn't been completed from a button-press action and complete it.

Hope it makes sense. Good luck!

like image 116
iandayman Avatar answered Oct 18 '22 14:10

iandayman