-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainThreadTask.cs
49 lines (41 loc) · 1.2 KB
/
MainThreadTask.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Threading.Tasks;
using UnityEngine;
namespace Gilzoide.MainThreadTask
{
public static class MainThreadTask
{
public static TaskScheduler Scheduler { get; private set; }
public static TaskFactory Factory { get; private set; }
public static async void Run(Action action)
{
await RunAsync(action);
}
public static Task RunAsync(Action action)
{
return Factory.StartNew(action);
}
public static Task<T> RunAsync<T>(Func<T> func)
{
return Factory.StartNew(func);
}
public static void InvokeOnMainThread(this Action action)
{
Run(action);
}
public static Task InvokeOnMainThreadAsync(this Action action)
{
return RunAsync(action);
}
public static Task<T> InvokeOnMainThreadAsync<T>(Func<T> func)
{
return RunAsync(func);
}
[RuntimeInitializeOnLoadMethod]
private static void InitializeFactory()
{
Scheduler = TaskScheduler.FromCurrentSynchronizationContext();
Factory = new TaskFactory(Scheduler);
}
}
}