zoukankan      html  css  js  c++  java
  • C#定时任务采用线程和队列实现

    构思:写一个全局的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);
        }
    }
  • 相关阅读:
    html调用php
    MySQL安装下载
    MySQL默认安装下载
    MySQL安装下载
    搭建php环境
    面试官:聊聊对Vue.js框架的理解
    TCP、UDP、HTTP、SOCKET之间的区别与联系
    HTTP/1、HTTP/2、HTTP/3
    git教程
    从jQuery到Serverless,前端十四年挖了多少坑?
  • 原文地址:https://www.cnblogs.com/wwwzzg168/p/3570199.html
Copyright © 2011-2022 走看看