Simple queue application and simple queue Application

Source: Internet
Author: User

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 );}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.