XAML-Map-Control/MapControl/Shared/Timer.cs

48 lines
1.1 KiB
C#
Raw Normal View History

2022-01-12 23:56:05 +01:00
// XAML Map Control - https://github.com/ClemensFischer/XAML-Map-Control
2025-01-01 18:57:55 +01:00
// Copyright © Clemens Fischer
2022-01-12 23:56:05 +01:00
// Licensed under the Microsoft Public License (Ms-PL)
2024-05-22 11:25:32 +02:00
#if WPF
2024-05-22 21:20:49 +02:00
using System.Windows;
2024-05-22 11:25:32 +02:00
using System.Windows.Threading;
2024-05-20 23:24:34 +02:00
#elif UWP
2022-01-12 23:56:05 +01:00
using Windows.UI.Xaml;
2025-01-28 17:52:40 +01:00
#elif WINUI
global using DispatcherTimer = Microsoft.UI.Dispatching.DispatcherQueueTimer;
using Microsoft.UI.Xaml;
2022-01-12 23:56:05 +01:00
#endif
2025-01-28 17:52:40 +01:00
using System;
2022-01-12 23:56:05 +01:00
namespace MapControl
{
internal static class Timer
{
2025-01-28 17:52:40 +01:00
public static DispatcherTimer CreateTimer(this DependencyObject obj, TimeSpan interval)
2022-01-12 23:56:05 +01:00
{
2025-01-28 17:52:40 +01:00
#if WINUI
var timer = obj.DispatcherQueue.CreateTimer();
#else
var timer = new DispatcherTimer();
#endif
timer.Interval = interval;
return timer;
2022-01-12 23:56:05 +01:00
}
public static void Run(this DispatcherTimer timer, bool restart = false)
{
if (restart)
{
timer.Stop();
}
2025-01-28 17:52:40 +01:00
#if WINUI
if (!timer.IsRunning)
#else
2022-01-12 23:56:05 +01:00
if (!timer.IsEnabled)
2025-01-28 17:52:40 +01:00
#endif
2022-01-12 23:56:05 +01:00
{
timer.Start();
}
}
}
}