C#Thread 使用后台 线程定时刷新 线程的使用 Thread的使用

97 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

private static Thread th;  
/// <summary>
/// 初始化
/// </summary>
public static void Init()
{
    th = new Thread(ThFun);
    th.IsBackground = true;//设为 后台线程
    th.Start();
} 
private static void ThFun()
{
    try
    {
        while (true)
        { 
            //执行方法()  ;
            Thread.Sleep(300000);//每五分钟刷新一次   以毫秒为单位
        }
    }
    catch { }
}

private void ThreadDemo()
{

   new Thread(() =>
            {
                while (true)
                {
                    try
                    {
                        OnThreadCheckUser();//要执行的方法
                    }
                    catch{}
                    Thread.Sleep(1000);
                }
            }) { IsBackground = true }.Start();

}

private void OnThreadCheckUser()
{
        MessageBox.Show("你需要的内容");

}

th.Start(); //启动线程
th.Suspend();//挂起线程
th.Resume();//继续线程
th.Abort();//中止线程