| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Threading;
- namespace Avalonia.Threading
- {
- /// <summary>
- /// SynchronizationContext to be used on main thread
- /// </summary>
- public class AvaloniaSynchronizationContext : SynchronizationContext
- {
- /// <summary>
- /// Controls if SynchronizationContext should be installed in InstallIfNeeded. Used by Designer.
- /// </summary>
- public static bool AutoInstall { get; set; } = true;
- /// <summary>
- /// Installs synchronization context in current thread
- /// </summary>
- public static void InstallIfNeeded()
- {
- if (!AutoInstall || Current is AvaloniaSynchronizationContext)
- {
- return;
- }
- SetSynchronizationContext(new AvaloniaSynchronizationContext());
- }
- /// <inheritdoc/>
- public override void Post(SendOrPostCallback d, object state)
- {
- Dispatcher.UIThread.InvokeAsync(() => d(state), DispatcherPriority.Send);
- }
- /// <inheritdoc/>
- public override void Send(SendOrPostCallback d, object state)
- {
- if (Dispatcher.UIThread.CheckAccess())
- d(state);
- else
- Dispatcher.UIThread.InvokeTaskAsync(() => d(state), DispatcherPriority.Send).Wait();
- }
- }
- }
|