I have to call a method every 5 minutes. I try with background agents that take 15 - 30 minutes in production mode. But I want to do this task within 5 minutes. How to do this task using threads? How assign this thread as background thread? How to do this process when current app not running also?
Asked
Active
Viewed 506 times
1 Answers
1
Use a System.Threading.Timer
.
Timer timer = new Timer(MyMethod,
state: null,
dueTime: 0,
period: 5*60*1000);
A timer will queue your method to be executed by the ThreadPool
every x milliseconds. Also, make sure you keep a strong reference to the timer to keep it from being garbage collected.
To run your method when the app itself is not running, you have to use background agents. There's no way to bypass the 15 minutes restriction.

dcastro
- 66,540
- 21
- 145
- 155
-
Timer will only run when app is in running on the mobile. and by pressing back button on the phone app will exit and the thread started in the app will no longer will run. – Muhammad Saifullah Jun 23 '14 at 12:16
-
@MuhammadSaifullah If you read the whole answer, you'll notice I mentioned that myself. The OP already knows how to make his method run while the app is not running. – dcastro Jun 23 '14 at 12:17
-
@MuhammadSaifullah The answer might not be the answer you wanted to hear, but it is the correct answer. – dcastro Jun 23 '14 at 12:18
-
OK you might me right :) help me to understand your point. in your answer you said that 'make sure you keep a strong reference to the timer' 1. when user exit the app... how the app will have that strong reference? 2. BackgorundTask: it is mentioned on msdn that windows phone Periodic agents typically run for 25 seconds(see below link). than how this to keep the reference of the timer? http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202942(v=vs.105).aspx – Muhammad Saifullah Jun 23 '14 at 12:31
-
@MuhammadSaifullah I was talking about keeping a strong reference to the timer *while* the app is running. If you don't the GC might collect the timer while your app is running, and your method won't be fired anymore. The background agent doesn't need a timer at all. – dcastro Jun 23 '14 at 12:35
-
@dcastro I want the timer thread running in the background. When I in another app, timer automatically call that specific method. – Dino Jun 23 '14 at 12:38
-
@Thesho In the question, you mentioned you can do this using background agents. That's it. Agents have to wait 15 mins in between runs, that's a Windows Phone limitation and there's nothing you can do about it. – dcastro Jun 23 '14 at 12:40
-
@dcastro I have to send data every 5 min through web api to server without this app running or app running in background. But I have to create this request from Clint side (WP8) not server in side. Do u have any idea to do this? – Dino Jun 23 '14 at 13:21
-
@Thesho As I've stated *twice* already, a Windows Phone application cannot do that unless it's running. You can do that as frequently as you want while the application is running; when it's not, you're capped at once per 15/30 minutes. That's a Windows Phone limitation. It might not be the answer you wanted to hear, but it's the correct answer. – dcastro Jun 23 '14 at 13:23
-
1+1 for the answer and your patience :P The official limit for both background agents and tasks in WP is 30 minutes, but background agents sometimes run more frequently (20 minutes it the most often I've seen). – yasen Jun 23 '14 at 18:20
-
@yasen Do you have any idea to do this task in kernel mode on windows phone 8? – Dino Jun 30 '14 at 11:26