-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadManager.cs
More file actions
49 lines (44 loc) · 1.64 KB
/
ThreadManager.cs
File metadata and controls
49 lines (44 loc) · 1.64 KB
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.Collections.Generic;
namespace GameServer
{
public class ThreadManager
{
private static readonly List<Action> executeOnMainThread = new List<Action>();
private static readonly List<Action> executeCopiedOnMainThread = new List<Action>();
private static bool actionToExecuteOnMainThread = false;
/// <summary>Sets an action to be executed on the main thread.</summary>
/// <param name="_action">The action to be executed on the main thread.</param>
public static void ExecuteOnMainThread(Action _action)
{
if (_action == null)
{
Console.WriteLine("No action to execute on main thread!");
return;
}
lock (executeOnMainThread)
{
executeOnMainThread.Add(_action);
actionToExecuteOnMainThread = true;
}
}
/// <summary>Executes all code meant to run on the main thread. NOTE: Call this ONLY from the main thread.</summary>
public static void UpdateMain()
{
if (actionToExecuteOnMainThread)
{
executeCopiedOnMainThread.Clear();
lock (executeOnMainThread)
{
executeCopiedOnMainThread.AddRange(executeOnMainThread);
executeOnMainThread.Clear();
actionToExecuteOnMainThread = false;
}
for (int i = 0; i < executeCopiedOnMainThread.Count; i++)
{
executeCopiedOnMainThread[i]();
}
}
}
}
}