Use System.Timers.Timer ??

haya5fhaya5f TRMember
edited February 2013 in Xamarin.Android

Hi .

After 3 seconds the textview text you want to exchange the project. code does not work. Where's the error I'm doing?

using System;

using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace AndroidApplication2
    [Activity(Label = "AndroidApplication2", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
        int count = 1;
        System.Timers.Timer t;
        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
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += delegate
                t = new System.Timers.Timer();
                t.Interval = 3000;
                t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);

        protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            TextView v = (TextView)FindViewById(Resource.Id.textView1);
            v.Text = "ege";


  • haya5fhaya5f TRMember

    v.Text = "ege"; -- freezes on this line

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    The problem is that you're modifying a UI-bound object from a non-UI thread.

    From the System.Timers.Timer documentation:

    The server-based Timer is designed for use with worker threads in a multithreaded environment. Server timers can move among threads to handle the raised Elapsed event...

    t_Elapsed() will not be executed from the UI thread, which means it is not safe for it to call FindViewById(), or touch v, or do anything else on this, for that matter, except RunOnUiThread().

  • haya5fhaya5f TRMember

    Thank you very much :)

  • Jalexkoop21Jalexkoop21 COMember

    Hi, I want that my application to close at 2 minutes. But when you restart activity change over time.
    How I can spend time from one activity to another?

  • SheepssjSheepssj BRMember

    I didnt udnerstand you Jalex but probably this will help.
    You can in some class in your app create a public static Timer yourTimer;
    And then you can reach it from anywhere in your application with


    Or explain better what you want

  • dana_Soranidana_Sorani IQMember

    thanks code work for me :)

Sign In or Register to comment.