zoukankan      html  css  js  c++  java
  • PetShop4.0学习笔记[01]:订单处理[01]——多线程程序结构

    多线程程序结构:

    using System;
    using System.Collections;
    using System.Configuration;
    using System.Threading;
    using System.Transactions;
    using PetShop.BLL;
    using PetShop.Model;
    
    namespace PetShop.OrderProcessor {
        /// <summary>
        /// We created this processor as a console application just to illustrate how to launch background threats
        /// to process Orders from MSMQ.  In a real application, we would implement this functionality as a Windows Services.
        /// </summary>
        class Program {
            // Variables from App Settings
            private static int transactionTimeout = int.Parse(ConfigurationManager.AppSettings["TransactionTimeout"]);
            private static int queueTimeout = int.Parse(ConfigurationManager.AppSettings["QueueTimeout"]);
            private static int batchSize = int.Parse(ConfigurationManager.AppSettings["BatchSize"]);
            private static int threadCount = int.Parse(ConfigurationManager.AppSettings["ThreadCount"]);
    
            private static int totalOrdersProcessed = 0;
    
            static void Main() {
    
                Thread workTicketThread;
                Thread[] workerThreads = new Thread[threadCount];
    
                for (int i = 0; i < threadCount; i++) {
    
                    workTicketThread = new Thread(new ThreadStart(ProcessOrders));
    
                    // Make this a background thread, so it will terminate when the main thread/process is de-activated
                    workTicketThread.IsBackground = true;
                    workTicketThread.SetApartmentState(ApartmentState.STA);
    
                    // Start the Work
                    workTicketThread.Start();
                    workerThreads[i] = workTicketThread;
                }
    
                Console.WriteLine("Processing started. Press Enter to stop.");
                Console.ReadLine();
                Console.WriteLine("Aborting Threads. Press wait...");
    
                //abort all threads
                for (int i = 0; i < workerThreads.Length; i++) {
    
                    workerThreads[i].Abort();
                }
    
                Console.WriteLine();
                Console.WriteLine(totalOrdersProcessed + " Orders processed.");
                Console.WriteLine("Processing stopped. Press Enter to exit.");
                Console.ReadLine();
            }
    
            /// <summary>
            /// Process a batch of asynchronous orders from the queue and submit them to the database within a transaction
            /// </summary>
            private static void ProcessOrders() {
    
                // the transaction timeout should be long enough to handle all of orders in the batch
                TimeSpan tsTimeout = TimeSpan.FromSeconds(Convert.ToDouble(transactionTimeout * batchSize));
    
                Order order = new Order();
                while (true) {
    
                    // queue timeout variables
                    TimeSpan datetimeStarting = new TimeSpan(DateTime.Now.Ticks);
                    double elapsedTime = 0;
    
                    int processedItems = 0;
    
                    ArrayList queueOrders = new ArrayList();
    
                    //OrderInfo orderData = orderQueue.Receive(timeout);
                    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, tsTimeout)) {
                        // Receive the orders from the queue
                        for (int j = 0; j < batchSize; j++) {
    
                            try {
                                //only receive more queued orders if there is enough time
                                if ((elapsedTime + queueTimeout + transactionTimeout) < tsTimeout.TotalSeconds) {
                                    queueOrders.Add(order.ReceiveFromQueue(queueTimeout));
                                }
                                else {
                                    j = batchSize;   // exit loop
                                }
    
                                //update elapsed time
                                elapsedTime = new TimeSpan(DateTime.Now.Ticks).TotalSeconds - datetimeStarting.TotalSeconds;
                            }
                            catch (TimeoutException) {
    
                                //exit loop because no more messages are waiting
                                j = batchSize;
                            }
                        }
    
                        //process the queued orders
                        for (int k = 0; k < queueOrders.Count; k++) {
                            order.Insert((OrderInfo)queueOrders[k]);
                            processedItems++;
                            totalOrdersProcessed++;
                        }
    
                        //batch complete or MSMQ receive timed out
                        ts.Complete();
                    }
    
                    Console.WriteLine("(Thread Id " + Thread.CurrentThread.ManagedThreadId + ") batch finished, " + processedItems + " items, in " + elapsedTime.ToString() + " seconds.");
                }
            }
        }
    }
    


     

  • 相关阅读:
    ans Single VIP LLB and SLB config
    Kindle支持文档类型
    RFC2544测试指标
    C#中equal与==的区别
    COGS 144. [USACO Dec07] 魅力手镯【01背包复习】
    我的第六个网页制作:table标签
    我的第五个网页制作:pre、html转义、abbr标签的使用
    我的第四个网页制作:列表标签
    我的第三个网页制作:b、i、s、u、sub、sup标签的使用
    我的第二个网页制作:p,hn,br标签的使用
  • 原文地址:https://www.cnblogs.com/WestGarden/p/3138394.html
Copyright © 2011-2022 走看看