27766

Cant redraw UILabel after updating text

Question:

I'm trying to create a timer which updates a label every tick, This is what I have so far:

public override void ViewDidLoad () { timer = new Timer (); timer.Elapsed += new ElapsedEventHandler (TimerOnTick); timer.Interval = 1000; timer.Start (); } private void TimerOnTick (object obj, EventArgs ea) { timerLabel.Text = DateTime.Now.ToString (); timerLabel.SetNeedsDisplay (); }

But this doesn't update the label. In debug I can see that timerLabel.Text is being set, but I cant get the view to update or redraw.

How can I get my view to redraw after I update my timerLabel.text?

Answer1:

The label is not being updated because System.Timers.Timer invokes the handler on a separate thread. You cannot change ui elements in a thread other than the main.

Enclose your handler code to be executed on the main thread:

private void TimerOnTick (object obj, EventArgs ea) { this.InvokeOnMainThread(delegate { timerLabel.Text = DateTime.Now.ToString (); timerLabel.SetNeedsDisplay (); }); }

Recommend

  • How to reference an icon from a Library Project in Application's AndroidManifest.xml
  • What makes the FederatedAuthentication.SessionAuthenticationModule return NULL?
  • How to enable formatted Xdebug errors and traces
  • unable to print elements in a linked list in c
  • Need help in implementing the Producer-Consumer problem with pthread and semaphore
  • Change baseDir attribute while using import tag
  • Using MSBuild with external xml parameter file
  • Exclude a specific term from product categories widget in Woocommerce
  • Dynamic form for submit values
  • Convert List to Image
  • how can i have scrollbar when position is negative?
  • getting exception when inserting events in android calendar
  • SSRS Subscription to an email group (distribution list) is not working
  • Installing Kohana on OpenShift?
  • nodeValue from DomDocument returning weird characters in PHP
  • How can I access the Google account user_id?
  • Custom Data Generator for Keras LSTM with TimeSeriesGenerator
  • Rust lifetime error
  • Reader monad - how does it conform to Monad interface?
  • internal javascript not works in angular2
  • How to make Rss News Reader application in android …? [closed]
  • How do I use libcurl to printf a remote FTP directory listing?
  • How to define something in JavaScript [closed]
  • C++ STL stack pop operation giving segmentation fault
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • How to clear a browser cache in Protractor
  • Drag and drop unicode TText in DelphiXe4
  • How to decleare char *const argv[] in swift [duplicate]
  • read part of h5 dataset python
  • Debug `Unexpected end of JSON input Error` on content script
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • How to get rgb from transparent pixel in js
  • Grails - How to implement a foreign key relationship not using an id column?
  • WPF custom control and direct content support
  • Write to .csv file with PHP (Commas in Data Error)
  • ReferenceError: TextEncoder is not defined
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}