locked
Timers and Screen refresh RRS feed

  • Question

  • User333966 posted

    I have an app that communicates with and monitors external hardware. (I am porting this from a Windows Store app)

    My app consists of several screen fragments.

    In my MainActivity, I created a System.Threading.Timer that is called every 100ms and handles the communications. [In the store app, this routine also updates various fields in the display.] In Xamarin.Android, when I tried to update the screen in this handler, I got an exception stating that "Only the original thread that created a view hierarchy can touch its views."

    I solved this by adding a System.Timers.Timer to my MainScreenFragment that is called every second to update the screen. This appeared to work, in that I could set a breakpoint and see that the field values were being changed. But in practice, the only time the screen was updated was when I touched or otherwise interacted with it. Also, not all fields were updated each tick.

    There are about 50 fields that need to be updated each second.

    How can I force the screen to update each second? I tried calling View.RefreshDrawableState() in my timer dispatch, but that did not seem to change anything.

    I looked at MVVMLight to see if binding the fields to the data might work, but it did not look like it would do what I needed. Does binding get me anything over my routine to manually update the fields, or is the overhead too high? Is there another way to implement data binding that would do the trick?

    Any suggestions? All help is appreciated.

    Dan

    Tuesday, August 15, 2017 2:24 AM

Answers

  • User333966 posted

    RunOnUiThread fixed the problem as Ljusnan suggested. It was just a pain to implement. (Docs??)

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 15, 2017 8:58 PM

All replies

  • User27078 posted

    To avoid the error you have to use RunOnUiThread like this:

    RunOnUiThread(() =>
    {
    //update views
    });
    
    Tuesday, August 15, 2017 8:32 AM
  • User333966 posted

    RunOnUiThread fixed the problem as Ljusnan suggested. It was just a pain to implement. (Docs??)

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, August 15, 2017 8:58 PM