locked
How to update a ViewText every 0.1 sec? RRS feed

  • Question

  • User119196 posted

    Dear community,

    this is my first post, I hope I'm right here.

    Aim: A ViewText which displays an int variable (ofc I converted it at first) and update every 1/10 second. So I have a timer which shows every 0.1 second.

    Problem: The text gets updated very often, but not properly. Its jumps 9 - 49 - 58 -73 - 120... (maybe a bit more steps, but that's how it seems to me). The code (I am not proud of it, but I am new to this stuff. I'm thankful for comprehension and advices):

    public string seconds = "";
    public int time = 0;
    
    t1 = new System.Timers.Timer();
                t1.Interval = 100;
                t1.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
                seconds= Convert.ToString(count);
    
    protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
                this.time = time+ 1;
                seconds= Convert.ToString (time);
                TextView tv1 = FindViewById<TextView> (Resource.Id.textView1);
                tv1.Text = string.Format (sekunden);
            }
    

    (this is ofc not the entire code, just the part which is related to the timer thing..)

    Monday, April 6, 2015 1:05 AM

Answers

  • User112 posted

    There is another approach (draft code)

    private CancellationTokenSource cts;
    public void StartUpdate()
    { 
        if (cts != null) cts.Cancel();
        cts = new CancellationTokenSource ();
        var ignore = UpdateAsync(cts.Token);
    }
    
    public void StopUpdate()
    {
        if (cts != null) cts.Cancel();
        cts = null;
    }
    
    public async Task UpdaterAsync()
    {
      while (!ct.IsCancellationRequested) {
         yourTextView.Text = whatever;
         await Task.Delay(100, ct);
      }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 7, 2015 8:48 AM

All replies

  • User119196 posted

    I meant "TextView" ofc. Sorry, I was really tired atm I wrote this. ^^

    Monday, April 6, 2015 9:48 AM
  • User9089 posted

    [Activity (Label = "TextViewUpdate", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { System.Timers.Timer timer; int time = 0; bool started = false; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); SetContentView (Resource.Layout.Main); timer = new System.Timers.Timer (1000); timer.Elapsed += (sender, e) => { time++; RunOnUiThread(() => { FindViewById<TextView>(Resource.Id.textView1).Text = time.ToString(); }); }; Button btn1 = FindViewById<Button> (Resource.Id.btnStart); btn1.Click += (object sender, EventArgs e) => { if(!started) { this.timer.Start(); btn1.Text = "Stop"; this.started = true; } else { this.timer.Stop(); btn1.Text = "Start"; this.started = false; } }; } }

    Tuesday, April 7, 2015 8:41 AM
  • User112 posted

    There is another approach (draft code)

    private CancellationTokenSource cts;
    public void StartUpdate()
    { 
        if (cts != null) cts.Cancel();
        cts = new CancellationTokenSource ();
        var ignore = UpdateAsync(cts.Token);
    }
    
    public void StopUpdate()
    {
        if (cts != null) cts.Cancel();
        cts = null;
    }
    
    public async Task UpdaterAsync()
    {
      while (!ct.IsCancellationRequested) {
         yourTextView.Text = whatever;
         await Task.Delay(100, ct);
      }
    }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, April 7, 2015 8:48 AM
  • User165104 posted

    ``` using System.Timers;

    private int _countSeconds;

    //This code into OnCreate method Timer _timer = new Timer (); _timer.Interval = 1000; _timer.Elapsed += OnTimedEvent; _countSeconds = 5; _timer.Enabled = true;

    private void OnTimedEvent (object sender, ElapsedEventArgs e) { countSeconds--; if (countSeconds < 0) { _timer.Stop (); } else { _context.RunOnUiThread(() => { FindViewById(Resource.Id.textView).Text = _countSeconds.ToString(); });
    } }

    ```

    Tuesday, May 31, 2016 4:50 PM