summaryrefslogtreecommitdiff
path: root/product/service.infrastructure/threading/SynchronousCommandProcessor.cs
blob: 87098aa4184617ba945c0b4c729f623aed60c1e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using gorilla.commons.utility;

namespace MoMoney.Service.Infrastructure.Threading
{
    public class SynchronousCommandProcessor : CommandProcessor
    {
        readonly Queue<Command> queued_commands;

        public SynchronousCommandProcessor()
        {
            queued_commands = new Queue<Command>();
        }

        public void add(Expression<Action> action_to_process)
        {
            add(new AnonymousCommand(action_to_process));
        }

        public void add(Command command_to_process)
        {
            queued_commands.Enqueue(command_to_process);
        }

        public void run()
        {
            while (queued_commands.Count > 0) queued_commands.Dequeue().run();
        }

        public void stop()
        {
            queued_commands.Clear();
        }
    }
}