• 简单的队列应用


    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);
            }
    
        }
    }
  • 相关阅读:
    python和matlab哪个难?看这篇就够了
    C语言怎么入门?(小白进)
    零基础学习单片机必看的一些知识点
    什么?Windows 里也可以访问 Linux 子系统文件了?
    智能小车开发的重点之电机该如何选型
    8种必知pcb电路原理图的绘制方法,你会吗?
    硬件工程师必备电路设计工具。进来,考考你?
    Leetcode-122. 买卖股票的最佳时机 II
    Leetcode-33. 搜索旋转排序数组
    Leetcode-236. 二叉树的最近公共祖先
  • 原文地址:https://www.cnblogs.com/tangchun/p/7930058.html
Copyright © 2020-2023  润新知