_Samples/Example_QueueProcessor.cs

(开头部分) 3KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

using System;
using System.Collections.Generic;

using ComLib;
using ComLib.Queue;
using ComLib.Application;


namespace ComLib.Samples
{
    /// <summary>
    /// Example for the Queue namespace.
    /// </summary>
    public class Example_QueueProcessor : App
    {
		
        class Quote
        {
            public Quote(string symbol, int price) 
            {
                Symbol = symbol;
                Price = price;
            }

            public string Symbol;
            public int Price;
        }



        /// <summary>
        /// Run the application.
        /// </summary>
        public override BoolMessageItem Execute()
        {
            // NOTE:
            // 1. Just showing the API for queue processing using lamdas as delegates.
            // 2. Typically the Queues.Process method will be called by a scheduler to process something periodically.
    
            // 1. Add queue processing handler for strings WITH OUT specifying a name.
            //    By default, the items in teh queues are processed 5 at a time on each call to Process.            
            Queues.AddProcessorFor<string>(items => items.ForEach(item => Console.WriteLine(item)));
            Queues.Enqueue<string>(new List<string>() { "a", "b", "c", "d", "e", "1", "2", "3", "4", "5" });

            // 1st call only prints a-e
            // 2nd call prints the remainder 1-5
            Queues.Process<string>();
            Queues.Process<string>();
            Console.WriteLine();
            
            // 2. Add queue processing by specifying the handler name.
            //    This is ideal if you have 2 handlers for the same type.            
            Queues.AddProcessorFor<int>("my_handler", items => items.ForEach(i => Console.WriteLine(i)));
            Queues.Enqueue<int>("my_handler", new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            Queues.Process("my_handler");
            Console.WriteLine();

            // 3. Add queue processing custom type.
            Queues.AddProcessorFor<Quote>(items => PrintQuotes(items));
            Queues.Enqueue<Quote>(new List<Quote>() { new Quote("MSFT", 20), new Quote("GOOG", 300), new Quote("CITI", 0),
后面还有 21 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。