简单的队列应用_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 简单的队列应用

简单的队列应用

 2017/11/30 18:04:32  _York  程序员俱乐部  我要评论(0)
  • 摘要:usingNewtonsoft.Json;usingSystem;usingSystem.Collections.Concurrent;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Threading;usingSystem.Web;namespaceTestQuenu{///<summary>///文件转码简单队列///</summary>publicclassTransCodQuenuHelp{///<
  • 标签:应用 队列
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);
        }

    }
}

 

发表评论
用户名: 匿名