Search code examples
c#xamarinxamarin.androidxamarin-zebblezebble

How Can I get date and time from the server on background process in Zebble for Xamarin?


I want to get some data like date and time on background process and update UI with that without block the UI process, I tried to do that with this code:

var dateTime = await Api.Get<DateTime>("api/getNow");
MyTextView.Text = dateTime;

But this code blocked the UI and I need to get this data each 30 minute and because of that I add code below which I found from Xamarin:

TimerCallback timerDelegate = new TimerCallback(CheckStatus);

Timer timer = new Timer(timerDelegate, s, 30000, 30000);

But in Zebble the Timer not defined with 4 parameters.


Solution

  • To run some code on the UI you can use this code:

    Device.UIThread.Invoke(()=>{    //UI code   });
    

    And for running some code on the background process and you need do something when UI is working on the other process you can use ThreadPool like below:

    Device.ThreadPool.Invoke(()=>{ //Background Code });
    

    After that, when you want to run some code in a period you can use the Timer in Thread namespace like below:

    var s = new TimerExampleState();
    var timerDelegate = new TimerCallback(() =>
    {
         //Your code
    });
    var timer = new Timer(timerDelegate, s, 1000, 1000);
    

    For more information you can see link below: http://zebble.net/docs/understanding-zebble-threading

    Disclaimer: I am a Zebble project contributor and engage in technical support.