Forum Xamarin.Android

help me to resolve issue with Timer elapsed event in Android

sasikiranvarikuntasasikiranvarikunta USMember ✭✭
edited June 2016 in Xamarin.Android

While debugging the application, timer is working as expected.
After deploying the application in device. if I keep the application in background, then timer elapsed event is not triggering at given time interval. I have implemented sample code, which displays timer start time and every minute it will update the UI with start time and current time with count (Count should be the number of minutes after start button click). Output for 6 minutes should increment to count to 6, but it is not working as expected (Please find the attachment for the reference).

[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
int count = 0;
Button btnStart;
Button btnStop;
TextView txtStatus;
TextView txtStartOrStop;
System.Timers.Timer _timer;
DateTime startTime;

    protected override void OnCreate(Bundle bundle)

        // Set our view from the "main" layout resource

        // Get our button from the layout resource,
        // and attach an event to it
        btnStart = FindViewById<Button>(Resource.Id.btnStart);
        btnStop = FindViewById<Button>(Resource.Id.btnStop);
        txtStatus = FindViewById<TextView>(Resource.Id.txtStatus);
        txtStartOrStop = FindViewById<TextView>(Resource.Id.txtStartOrStop);
        btnStart.Click += StartTimer;
        btnStop.Click += StopTimer;

    public void StartTimer(object sender, EventArgs e)
        if (_timer == null)
            startTime = DateTime.Now;
            _timer = new System.Timers.Timer();
            _timer.Interval = 60000;
            _timer.Elapsed += bw_DoWork;
            _timer.Enabled = true;
            txtStartOrStop.Text = "Running";
            count = 0;
            RunOnUiThread(() => txtStatus.Text = startTime.ToLongTimeString() + "  " + count + "  " + DateTime.Now.ToLongTimeString());

    private void bw_DoWork(object sender, System.Timers.ElapsedEventArgs e)
        RunOnUiThread(() => txtStatus.Text = startTime.ToLongTimeString() + "  " + count + "  " + DateTime.Now.ToLongTimeString());

    public void StopTimer(object sender, EventArgs e)
        if (_timer != null)
            _timer = null;
            txtStartOrStop.Text = "";

Best Answer


  • ashalvaashalva GEMember ✭✭✭


    I had a similar problem and finally found out that timer event is not triggered in the background.
    So I did a little trick.
    Catch the exact time when the application goes into background, lets say you have backgroundMilliseconds.
    And when it comes to foreground => foregroundMilliseconds.

    Here is the pseudo code:
    if ((foregroundMilliseconds - backgroundMilliseconds) >= 6 Minutes) UpdateUI();

    You can get the exact duration of being in background mode.

    You can catch the app foreground and background mode in OnResume and OnPause correspondingly.

  • sasikiranvarikuntasasikiranvarikunta USMember ✭✭

    Hi @ashalva .. Thanks for the reply. My requirement is to do some operation at background for every one minute (with start and stop buttons). I need that operation to be performed even if the application is in background mode, so I made this sample application to share with other developers.

  • sasikiranvarikuntasasikiranvarikunta USMember ✭✭

    Hi @ashalva .... I tried with backgrounding service and System.Threading.Timer instead of System.Timers.Timer. Still I'm facing the same problem.. Not sure, it is issue with my device or code. I'm trying a lot to solve this issue. :(

  • ashalvaashalva GEMember ✭✭✭

    Changing the Timer Reference will not solve the problem. Have you implemented the backgound service correctly? You should see your service in app manager > services

  • sasikiranvarikuntasasikiranvarikunta USMember ✭✭

    Yes. Service is working fine.

  • sasikiranvarikuntasasikiranvarikunta USMember ✭✭

    Hi @ashalva.. Timer is not working properly when device went to sleep mode. I was using location service to get the updates in my application.. I updated time in location update event to trigger at every one minute. This is not 100% accurate but it is working better compared to timers. Thanks for the help :)

  • ashalvaashalva GEMember ✭✭✭


    You are welcome!

Sign In or Register to comment.