Explorar el Código

优化 World 类的 FrameInfo 方法和控制台输出

修改了 World 类中的 FrameInfo 方法,移除了控制台输出的代码,并将 WorkTimes 对象存储在一个静态的 ConcurrentDictionary<World, WorkTimes> 中。添加了一个静态的 Print 方法,该方法会将所有 WorldsInfo 中的 WorkTimes 信息拼接成一个字符串,并在控制台上打印。如果信息没有变化,则不会重复打印。在 World 类的某个方法中,添加了一个新的任务,该任务会每 100 毫秒调用一次 Print 方法,并且每 30 次调用会清空一次控制台。
林豪 左 hace 11 meses
padre
commit
91aacd2f20
Se han modificado 1 ficheros con 34 adiciones y 4 borrados
  1. 34 4
      WCS.Core/World.cs

+ 34 - 4
WCS.Core/World.cs

@@ -31,14 +31,28 @@ public abstract class World : DescriptionClass
         if (sys == null) throw new Exception($"世界{GetType().Name}中不存在系统{typeof(T).Name}");
         return sys;
     }
-
+    static ConcurrentDictionary<World, WorkTimes> WorldsInfo = new ConcurrentDictionary<World, WorkTimes>();
     protected virtual void FrameInfo(WorkTimes wt)
     {
-        if (wt.Total > Interval) Console.ForegroundColor = ConsoleColor.Red;
-        Console.WriteLine(wt.GetInfo());
-        Console.ResetColor();
+        WorldsInfo[this] = wt;
+        //if (wt.Total > Interval) Console.ForegroundColor = ConsoleColor.Red;
+        //Console.WriteLine(wt.GetInfo());
+        //Console.ResetColor();
     }
+    static string LastInfo = "";
+    static void Print()
+    {
+        var info = "\n" + string.Join("\n", WorldsInfo.Values.Select(v => v.GetInfo().PadRight(200))).PadRight(1000);
 
+        if (info != LastInfo)
+        {
+            Console.CursorVisible = false;
+            Console.WriteLine(info);
+            Console.SetCursorPosition(0, 0);
+            LastInfo = info;
+        }
+
+    }
     public void Log<T>(T log) where T : ILog
     {
         var channel = Ltc.GetChannel();
@@ -140,6 +154,22 @@ public abstract class World : DescriptionClass
         }
 
         IsStart = true;
+
+        Task.Run(() =>
+        {
+
+            var i = 0;
+            while (IsStart)
+            {
+                i++;
+                if (i % 30 == 1)
+                {
+                    Console.Clear();
+                }
+                Print();
+                Task.Delay(100).Wait();
+            }
+        });
     }
 
     public static void StopAll()