• 简单的队列应用


    using Newtonsoft.Json;
    using System;
    using System.Collections.Concurrent;
    using System.Diagnostics;
    using System.IO;
    using System.Threading;
    using System.Web;
    
    namespace TestQuenu
    {
        /// <summary>
        /// 文件转码简单队列
        /// </summary>
        public class TransCodQuenuHelp
        {
    
            /// <summary>
            /// 待转码的文件队列
            /// </summary>
            private static ConcurrentQueue<string> waitTransFileQuene = new ConcurrentQueue<string>();
    
            /// <summary>
            /// 系列化文件存放位置
            /// </summary>
            private static string waitTransFileSavePath = HttpContext.Current.Server.MapPath("~/1.txt");
    
            /// <summary>
            /// 等待毫秒数
            /// </summary>
            private const int waitTime = 300000;
    
            /// <summary>
            /// 日志来源
            /// </summary>
            private const string source = "";
    
            /// <summary>
            /// 日志名称
            /// </summary>
            private const string logName = "";
    
            /// <summary>
            /// 初始化队列
            /// </summary>
            public static void init()
            {
                initLog();
                ReadWaitTransFile();
    
                /*开启线程用于定时任务*/
                Thread thread = new Thread(new ThreadStart(ListenceTransCode));
                thread.Start();
                thread.IsBackground = true;
            }
    
            /// <summary>
            /// 初始化日志
            /// </summary>
            private static void initLog()
            {
                if (!EventLog.SourceExists(source))
                {
                    EventLog.CreateEventSource(source, logName);
                }
            }
    
    
            /// <summary>
            /// 转码监听
            /// </summary>
            private static void ListenceTransCode()
            {
                while (true)
                {
                    if (!waitTransFileQuene.IsEmpty)
                    {
                        string curTransFile = string.Empty;
                        bool isSuc = waitTransFileQuene.TryDequeue(out curTransFile);
                        if (isSuc)
                        {
                            //转码
                            try
                            {
                                Stopwatch stopwatch = new Stopwatch();
                                stopwatch.Start();
    
                                //doSomething();
    
                                stopwatch.Stop();
                                TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间
                                string msg = string.Format("{0}成功,用时{1}分钟", curTransFile, timespan.TotalMinutes);
                                EventLog.WriteEntry(source, msg, EventLogEntryType.Information);
                            }
                            catch (Exception ex)
                            {
                                EventLog.WriteEntry(source, ex.Message, EventLogEntryType.Error);
                            }
                        }
                    }
                    else
                    {
                        Thread.Sleep(waitTime);
                    }
                }
            }
    
            /// <summary>
            /// 从文件中读取未转码的文件列表
            /// </summary>
            private static void ReadWaitTransFile()
            {
                if (File.Exists(waitTransFileSavePath))
                {
                    string waitTransFile = File.ReadAllText(waitTransFileSavePath);
                    waitTransFileQuene = JsonConvert.DeserializeObject<ConcurrentQueue<string>>(waitTransFile);
                }
            }
    
            /// <summary>
            /// 保存未转码的文件类别到文件
            /// </summary>
            public static void SaveWaitTransFile()
            {
                if (!waitTransFileQuene.IsEmpty)
                {
                    File.WriteAllText(waitTransFileSavePath, JsonConvert.SerializeObject(waitTransFileQuene));
                }
            }
    
            /// <summary>
            /// 添加新的任务
            /// </summary>
            /// <param name="fileName">文件全名</param>
            public static void Add(string fileName)
            {
                waitTransFileQuene.Enqueue(fileName);
            }
    
        }
    }
  • 相关阅读:
    mybatis入门_一对多,多对多映射以及整合spring框架
    mybatis入门_配置文件的配置
    mybatis入门_mybatis基本原理以及入门程序
    BOM 3.1 location对象 | history对象 | navigator对象 | 定时器 | 三大系列
    Sublime Text Build 3207 x64 无法安装Package Control和插件
    无法启动此程序,因为计算机中丢失api-ms-win-crt-runtime-l1-1-0.dll已解决
    测试
    ECMAScript1.4 对象 | 简单数据类型与复杂数据类型 | 内置对象 | 基本包装类型 | String
    ECMAScript1.3 数组 | 函数 | 作用域 | 预解析
    webAPI(DOM) 2.1 获取页面元素 | 事件1 | 属性操作 | 节点 | 创建元素 | 事件2
  • 原文地址:https://www.cnblogs.com/tangchun/p/7930058.html
Copyright © 2020-2023  润新知