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();
}
}
}
C#在新的线程中执行逻辑
最新推荐文章于 2023-08-18 21:40:35 发布