Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
Dispatcher.cs
浏览该文件的文档.
1namespace Engine {
2 public static class Dispatcher {
3 public struct ActionInfo {
4 public Action Action;
5
6 public ManualResetEventSlim Event;
7 }
8
9 static int? m_mainThreadId;
10
11 static List<ActionInfo> m_actionInfos = [];
12
13 static List<ActionInfo> m_currentActionInfos = [];
14
15 public static int MainThreadId => m_mainThreadId ?? throw new InvalidOperationException("Dispatcher is not initialized.");
16
17 public static void ExecuteActionsOnCurrentThread() {
19 lock (m_actionInfos) {
21 m_actionInfos.Clear();
22 }
23 foreach (ActionInfo currentActionInfo in m_currentActionInfos) {
24 try {
25 currentActionInfo.Action();
26 }
27 catch (Exception ex) {
28 Log.Error("Dispatched action failed. Reason: {0}", ex);
29 }
30 finally {
31 if (currentActionInfo.Event != null) {
32 currentActionInfo.Event.Set();
33 }
34 }
35 }
36 }
37
38 public static void Dispatch(Action action, bool waitUntilCompleted = false) {
39 if (!m_mainThreadId.HasValue) {
40 throw new InvalidOperationException("Dispatcher is not initialized.");
41 }
42 ActionInfo actionInfo;
43 if (m_mainThreadId.Value == Environment.CurrentManagedThreadId) {
44 action();
45 }
46 else if (waitUntilCompleted) {
47 actionInfo = default;
48 actionInfo.Action = action;
49 actionInfo.Event = new ManualResetEventSlim(false);
50 ActionInfo item = actionInfo;
51 lock (m_actionInfos) {
52 m_actionInfos.Add(item);
53 }
54 item.Event.Wait();
55 item.Event.Dispose();
56 }
57 else {
58 lock (m_actionInfos) {
59 List<ActionInfo> actionInfos = m_actionInfos;
60 actionInfo = new ActionInfo { Action = action };
61 actionInfos.Add(actionInfo);
62 }
63 }
64 }
65
66 internal static void Initialize() {
67 m_mainThreadId = Environment.CurrentManagedThreadId;
68 }
69
70 internal static void BeforeFrame() {
72 }
73
74 internal static void AfterFrame() { }
75 }
76}
System.Environment Environment
static void AfterFrame()
static void Dispatch(Action action, bool waitUntilCompleted=false)
static ? int m_mainThreadId
static void ExecuteActionsOnCurrentThread()
static void BeforeFrame()
static void Initialize()
static List< ActionInfo > m_actionInfos
static List< ActionInfo > m_currentActionInfos
static void Error(object message)
定义 Log.cs:80
ManualResetEventSlim Event