Dispatcher/Threading
- Dominant language
- No language data
- Stars
- 282
- Forks
- 265
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 19
Description
Xamarin.Forms had the Device.* methods to start timers, marshal to different threads etc. These are obsolete in .NET MAUI.
Instead, .NET MAUI gains a `Dispatcher` object.
Timer example:
```
var dispatcher = Application.Current.Dispatcher; // if your context isn't a BindableObject (if your context is a BO then just this.Dispatcher...)
var timer = dispatcher.CreateTimer();
timer.Interval = TimeSpan.FromSeconds(1);
timer.Tick += (s, e) => this.DateTime = DateTime.Now;
timer.Start();
```
Or: https://github.com/davidbritch/dotnet-maui-samples/blob/main/UserInterface/Views/ButtonDemos/ButtonDemos/Views/PressAndReleaseButtonPage.xaml.cs
Or `System.Threading.Timer` from a view model: https://github.com/davidbritch/dotnet-maui-samples/blob/main/XAML/XamlSamples/XamlSamples/ViewModels/ClockViewModel.cs
Or `PeriodicTimer` if you don't want to use callbacks.
Implementation: https://github.com/dotnet/maui/tree/main/src/Core/src/Dispatching
PR: https://github.com/dotnet/maui/pull/4944
From:
- native code: native things like UIView.BeginInvoke
- xplat from a UI context: BindableObject.Dispatcher.Dispatch
- xplat from a VM: (dodgy) but Application.Current.Dispatcher.Dispatch or MainThread.InvokeOnMainThread (you could also just pass in IDispatcher as a ctor arg)
in order of hackyness
Contributor guide
Assessment
This issue has not been assessed yet.