C#在新的线程中执行逻辑

xiaoxiao2021-02-28  103

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace Thread { public class example { public static void 本地刷新ToolStripMenuItem_Click(object sender, EventArgs e) { // 在新的线程中执行逻辑 ThreadTool.ThreadRun(UpdateLocal); } private static void UpdateLocal() { // 自定义处理逻辑 } } public class ThreadTool { /// <summary> /// 定义委托接口处理函数,用于实时处理cmd输出信息 /// </summary> public delegate void Method(); /// <summary> /// 在新的线程中执行method逻辑 /// </summary> public static void ThreadRun(Method method) { Thread thread = new Thread(delegate() { // 允许不同线程间的调用 Control.CheckForIllegalCrossThreadCalls = false; // 执行method逻辑 if (method != null) method(); }); thread.Priority = ThreadPriority.AboveNormal; // 设置子线程优先级 Thread.CurrentThread.Priority = ThreadPriority.Highest; // 设置当前线程为最高优先级 thread.Start(); } } }
转载请注明原文地址: https://www.6miu.com/read-55135.html

最新回复(0)