线程 定时任务 实现思路

  关于C#中timer类,在C#里关于定时器类就有3个,

分别是System.Windows.Forms,System.Timers.Timer和System.Threading.Timer

1、xml文件配置执行周期,以毫秒为单为:1天=24*60*60*1000=86400000

2、

构思:写一个全局的Queue ,  然后开一个线程去循环. 不善言语,直接看代码吧!
 

public class MyQueue
{
        
public decimal Id { getset; }
        
public decimal Type { getset; }
}

static Queue<MyQueue> TaskQueue = new Queue<MyQueue>();

static void Main(string[] args)
{
    ServiceStart();
    Console.ReadLine();
}

public static void ServiceStart()
{
    Thread TaskThread 
= new Thread(new ThreadStart(ThreadInvoke));
    TaskThread.IsBackground 
= true;
    TaskThread.Start();
}
public static void ThreadInvoke()
{
    
while (true)
    {  
        
string m_time = DateTime.Now.ToString("HH:mm:ss");
        
if (m_time == Invoke_Time) //判断是否指定时间(Invoke_Time)
        {
            
//InitTaskQueue();//初始化队列
            while (TaskQueue.Count > 0)
            {
                MyQueue m_mq 
= null;
                
lock (TaskQueue)
                {
                    m_mq 
= TaskQueue.Dequeue();
                }
                
//YourMethd(m_mq.UserId, m_mq.FeedTypeId); //调用方法
                
//Common.Log.Logger.Info(DateTime.Now.ToString()); //记录日志
                Thread.Sleep(100);
            }
        }
        Thread.Sleep(
100);
    }
}

2、单独aspx页,手动执行定时任务,以及修改配置参数;显示最后更新时间,操作人;

 完整demo:

/**
 * 在ASP.net的WEB开发中经常会碰到这样的一个问题:即用户操作响应慢的情况。
 * 这时候就可以用后台定时任务来提前实现,并且将结果存放好或分解成响应快的任务。
 * 可以采用Asp.net的定时处理方式,直接在WEB服务器层来进行处理。
 *
 * 关于C#中timer类,在C#里关于定时器类就有3个,
 * 分别是System.Windows.Forms,System.Timers.Timer和System.Threading.Timer
 * 在这里我们主要使用System.Threading.Timer,
 * 因为它是一个使用回调方法的计时器,而且由线程池线程服务,简单且对资源要求不高
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using MyIBatisNet;
using SystemUI.commen;
using MyIBatisNet.BizLogic;

namespace SystemUI.ScheduledTask
{
    public class ScheduledTask
    {

        private static readonly ScheduledTask _ScheduledTask = null;
        private System.Threading.Timer UpdateTimer = null;
        private int Interval = 1 * 1000;//间隔时间,这里设置为15分钟
        private int _IsRunning;//上一个时间间隔触发的任务是否运行完成

        static ScheduledTask()
        {
            _ScheduledTask = new ScheduledTask();
        }

        public static ScheduledTask Instance()
        {
            return _ScheduledTask;
        }

        private void DelTimerCallback(object sender)
        {
            if (Interlocked.Exchange(ref _IsRunning, 1) == 0)
            {
                try
                {
                    //JSHelper.Alert("test");
                    //定时做的任务
                    QCCInfoHelperBLL qccInfoHelperbll = new QCCInfoHelperBLL();
                    qccInfoHelperbll.DelQCCInfoTop5();
                    //执行操作完成,记录日志方便查询
                }
                catch (Exception ex)
                {
                    //写入错误日志...
                }
                finally
                {
                    Interlocked.Exchange(ref _IsRunning, 0);
                }
            }
        }

        private void ThreadTimerCallback()
        {
            QCCInfoHelperBLL qccInfoHelperbll = new QCCInfoHelperBLL();
            qccInfoHelperbll.DelQCCInfoTop5();
        }


        public void Start()
        {


            //Thread myThread = new Thread(new ThreadStart(ThreadTimerCallback));
            //myThread.Start();

            if (UpdateTimer == null)
            {
                UpdateTimer = new System.Threading.Timer(new TimerCallback(DelTimerCallback),
                    null, Interval, 5000);
               
            }
            //(上面用的不是线程,休眠无关)当前线程挂起10秒(1000毫秒:等1秒)

            //Thread.Sleep(10000);
           
        }

        public void Stop()
        {
            if (UpdateTimer != null)
            {
                UpdateTimer.Dispose();
                UpdateTimer = null;
            }
        }

    }
}

原文地址:https://www.cnblogs.com/p_db/p/2399541.html