I want to run a block of code similar to the following code. The purpose of the code is to make an HTTP request at a one-second period without blocking the UI thread.
private void GetCodeFromTheServer()
{
WebClient client = new WebClient();
string code = client.DownloadString(new Uri(@"http://example.com/code"));
Toast.MakeText(this, "Code: " + code, ToastLength.Long).Show();
}
To get started, here is the basic timer. Device. StartTimer(TimeSpan. FromSeconds(30), () => { // Do something return true; // True = Repeat again, False = Stop the timer });
If you need to do something at 1 second intervals, you might be able to use a timer for that. I have used code like this in Xamarin.Android myself:
private void CountDown ()
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.Elapsed += OnTimedEvent;
timer.Enabled = true;
}
private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
{
}
OnTimedEvent will then fire every second and you can do your call in an async Task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With