private void StartTimer() { var dispatchSourceTimer = new DispatchSource.Timer(DispatchQueue.MainQueue); var delay = 2 * NanosecondsInSecond; var leeway = 5 * NanosecondsInSecond; dispatchSourceTimer.SetTimer(DispatchTime.Now, delay, leeway); dispatchSourceTimer.SetRegistrationHandler(() => PrintResult("Timer registered")); dispatchSourceTimer.SetEventHandler(() => PrintResult("Timer tick")); dispatchSourceTimer.SetCancelHandler(() => PrintResult("Timer stopped")); dispatchSource = dispatchSourceTimer; dispatchSource.Resume(); }
public override async void ViewDidLoad() { base.ViewDidLoad(); // Perform any additional setup after loading the view, typically from a nib. await CheckAndRequestPermissionAsync(new Permissions.LocationAlways()); _timer = new DispatchSource.Timer(DispatchQueue.GetGlobalQueue(DispatchQueuePriority.Background)); _timer.SetEventHandler(async() => { var location = await Geolocation.GetLocationAsync(new GeolocationRequest() { Timeout = TimeSpan.FromSeconds(5), DesiredAccuracy = GeolocationAccuracy.Best }); System.Diagnostics.Debug.WriteLine($"{location?.Latitude}, {location?.Longitude}, {DateTime.Now}"); }); }
public DispatchTimer(long delay, DispatchQueue queue, TimerHandler block) { this.timerBlock = block; this.queue = queue; this.delay = delay; this.source = new DispatchSource.Timer(queue); base.Init(); Action wrapper = () => { if (!source.IsCanceled) { source.Cancel(); timerBlock(this); } }; wrappedBlock = wrapper; }