From a4efcb174e7a6d1411ba6722b2e4ffc0f585d2fb Mon Sep 17 00:00:00 2001 From: Elivo Date: Mon, 28 Apr 2025 15:22:01 +0800 Subject: [PATCH] =?UTF-8?q?RuntimeUtility=EF=BC=9AInBackground=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E9=87=8D=E8=BD=BD=EF=BC=8C=E5=8F=AF=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E6=97=B6=E5=B8=B8=EF=BC=8C=E7=B1=BB=E4=BC=BC?= =?UTF-8?q?=20JS=20=E7=9A=84=20setTimeout=20=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/RuntimeUtility.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Apewer/RuntimeUtility.cs b/Apewer/RuntimeUtility.cs index 836cf7e..316f5e9 100644 --- a/Apewer/RuntimeUtility.cs +++ b/Apewer/RuntimeUtility.cs @@ -629,6 +629,25 @@ namespace Apewer return thread; } + /// 在后台线程中执行,指定 Try 将忽略 Action 抛出的异常。 + /// 延迟的毫秒数。 + /// 要执行的 Action。 + /// 忽略 Action 抛出的异常。 + /// 对返回的 Thread 调用 Abort 可结束线程的执行。 + [MethodImpl(MethodImplOptions.NoInlining)] + public static Thread InBackground(int delay, Action action, bool @try = false) + { + if (action == null) return null; + var thread = new Thread(delegate (object v) + { + if (delay > 0) Thread.Sleep(delay); + Invoke(() => ((Action)v)(), @try); + }); + thread.IsBackground = true; + thread.Start(action); + return thread; + } + /// 启动线程,在新线程中执行。 /// 要执行的 Action。 /// 设置线程为后台线程。