Simple queue application and simple queue Application
Using Newtonsoft. json; using System. collections. concurrent; using System. diagnostics; using System. IO; using System. threading; using System. web; namespace TestQuenu {// <summary> // simple file transcoding queue // </summary> public class TransCodQuenuHelp {// <summary> // file to be transcoded queue /// </summary> private static ConcurrentQueue <string> waitTransFileQuene = new ConcurrentQueue <string> (); /// <summary> // series Location of the stored files /// </summary> private static string waitTransFileSavePath = HttpContext. Current. Server. MapPath ("~ /1.txt"); /// <summary> /// wait for the number of milliseconds /// </summary> private const int waitTime = 300000; /// <summary> /// log source /// </summary> private const string source = ""; /// <summary> /// log name /// </summary> private const string logName = ""; /// <summary> /// initialize the queue /// </summary> public static void init () {initLog (); ReadWaitTransFile (); /* enable threads for scheduled tasks */Thread thread = new Thread (new ThreadStart (ListenceTrans Code); thread. start (); thread. isBackground = true;} // <summary> // initialization log // </summary> private static void initLog () {if (! EventLog. sourceExists (source) {EventLog. createEventSource (source, logName) ;}/// <summary> // transcoding listener /// </summary> private static void ListenceTransCode () {while (true) {if (! WaitTransFileQuene. isEmpty) {string curTransFile = string. empty; bool isSuc = waitTransFileQuene. tryDequeue (out curTransFile); if (isSuc) {// transcode try {Stopwatch stopwatch = new Stopwatch (); stopwatch. start (); // doSomething (); stopwatch. stop (); TimeSpan timespan = stopwatch. elapsed; // obtain the total time measured by the current instance. string msg = string. format ("{0} succeeded, it took {1} minutes", curTransFile, timespan. totalMinutes); EventLog. writeEnt Ry (source, msg, EventLogEntryType. information);} catch (Exception ex) {EventLog. writeEntry (source, ex. message, EventLogEntryType. error) ;}} else {Thread. sleep (waitTime) ;}}/// <summary> /// read the list of untranscoded files from the file /// </summary> private static void ReadWaitTransFile () {if (File. exists (waitTransFileSavePath) {string waitTransFile = File. readAllText (waitTransFileSavePath); waitTransFileQuene = J SonConvert. deserializeObject <ConcurrentQueue <string> (waitTransFile );}} /// <summary> /// Save the untranscoded file category to the file // </summary> public static void SaveWaitTransFile () {if (! WaitTransFileQuene. isEmpty) {File. writeAllText (waitTransFileSavePath, JsonConvert. serializeObject (waitTransFileQuene ));}} /// <summary> /// Add a new task // </summary> /// <param name = "fileName"> file full name </param> public static void add (string fileName) {waitTransFileQuene. enqueue (fileName );}}}