Files
RobustToolbox/Robust.Shared/Asynchronous/TaskManager.cs
T
Pieter-Jan BriersandGitHub bb4a1eda8e Project file refactor (#819)
* Project file refactor

Move all the .csproj files to the new .NET Core style.
This doesn't make any difference for compiling for Framework,
but it does reduce a ton of useless boilerplate.

As an extension of this, killed a bunch of uncompiled & unmaintained .cs files.

Compiling for release (to profile) works now.
Removed AnyCPU targets from the solution file.

* Fix compiler warnings.
2019-05-28 00:16:01 +02:00

54 lines
1.5 KiB
C#

using System;
using System.Threading;
using Robust.Shared.Exceptions;
using Robust.Shared.IoC;
namespace Robust.Shared.Asynchronous
{
internal sealed class TaskManager : ITaskManager
{
private RobustSynchronizationContext _mainThreadContext;
#pragma warning disable 649
[Dependency] private readonly IRuntimeLog _runtimeLog;
#pragma warning restore 649
public void Initialize()
{
_mainThreadContext = new RobustSynchronizationContext(_runtimeLog);
SynchronizationContext.SetSynchronizationContext(_mainThreadContext);
}
public void ProcessPendingTasks()
{
_mainThreadContext.ProcessPendingTasks();
}
public void RunOnMainThread(Action callback)
{
_mainThreadContext.Post(_runCallback, callback);
}
private static readonly SendOrPostCallback _runCallback = o =>
{
((Action)o)();
};
}
public interface ITaskManager
{
void Initialize();
void ProcessPendingTasks();
/// <summary>
/// Run a delegate on the main thread sometime later.
/// Thread safe.
/// </summary>
/// <remarks>
/// Useful if you want to run a callback from a separate thread.
/// </remarks>
/// <param name="callback">The callback that will be invoked on the main thread.</param>
void RunOnMainThread(Action callback);
}
}