Commit b1d9d1c

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-03-27 03:41:46
trying to implement the asynchronous command processor.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@115 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent 4358093
Changed files (4)
trunk/product/MyMoney/Infrastructure/Threading/AsynchronousCommandProcessor.cs
@@ -0,0 +1,68 @@
+using System.Collections.Generic;
+using System.Threading;
+using MoMoney.Utility.Core;
+
+namespace MoMoney.Infrastructure.Threading
+{
+    public class AsynchronousCommandProcessor : ICommandProcessor
+    {
+        readonly Queue<ICommand> queued_commands;
+        readonly EventWaitHandle manual_reset;
+
+        public AsynchronousCommandProcessor()
+        {
+            queued_commands = new Queue<ICommand>();
+            manual_reset = new ManualResetEvent(false);
+        }
+
+        public void add(ICommand command_to_add_to_queue)
+        {
+            lock (queued_commands)
+            {
+                queued_commands.Enqueue(command_to_add_to_queue);
+                reset_thread();
+            }
+        }
+
+        public void run()
+        {
+            reset_thread();
+            new Thread(execute_commands).Start();
+        }
+
+        void execute_commands()
+        {
+            manual_reset.WaitOne();
+            next_command().run();
+            reset_thread();
+        }
+
+        ICommand next_command()
+        {
+            lock (queued_commands)
+            {
+                if (queued_commands.Count == 0)
+                {
+                    manual_reset.Reset();
+                    return new EmptyCommand();
+                }
+                return queued_commands.Dequeue();
+            }
+        }
+
+        void reset_thread()
+        {
+            lock (queued_commands)
+            {
+                if (queued_commands.Count > 0)
+                {
+                    manual_reset.Set();
+                }
+                else
+                {
+                    manual_reset.Reset();
+                }
+            }
+        }
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Infrastructure/Threading/CommandProcessor.cs
@@ -3,14 +3,9 @@ using MoMoney.Utility.Core;
 
 namespace MoMoney.Infrastructure.Threading
 {
-    public interface ICommandProcessor : ICommand
-    {
-        void add(ICommand command_to_add_to_queue);
-    }
-
     public class CommandProcessor : ICommandProcessor
     {
-        private readonly Queue<ICommand> queued_commands;
+        readonly Queue<ICommand> queued_commands;
 
         public CommandProcessor()
         {
@@ -24,7 +19,8 @@ namespace MoMoney.Infrastructure.Threading
 
         public void run()
         {
-            while (queued_commands.Count > 0) {
+            while (queued_commands.Count > 0)
+            {
                 queued_commands.Dequeue().run();
             }
         }
trunk/product/MyMoney/Infrastructure/Threading/ICommandProcessor.cs
@@ -0,0 +1,9 @@
+using MoMoney.Utility.Core;
+
+namespace MoMoney.Infrastructure.Threading
+{
+    public interface ICommandProcessor : ICommand
+    {
+        void add(ICommand command_to_add_to_queue);
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/MyMoney.csproj
@@ -277,9 +277,11 @@
     <Compile Include="Domain\Core\Months.cs" />
     <Compile Include="Infrastructure\Container\Windsor\configuration\ComponentRegistrationConfiguration.cs" />
     <Compile Include="Infrastructure\System\application_environment.cs" />
+    <Compile Include="Infrastructure\Threading\AsynchronousCommandProcessor.cs" />
     <Compile Include="Infrastructure\Threading\BackgroundThreadFactory.cs" />
     <Compile Include="Infrastructure\Threading\BackgroundThread.cs">
     </Compile>
+    <Compile Include="Infrastructure\Threading\ICommandProcessor.cs" />
     <Compile Include="Infrastructure\Threading\IntervalTimer.cs" />
     <Compile Include="Infrastructure\Threading\ISynchronizationContext.cs" />
     <Compile Include="Infrastructure\Threading\Juval\Synchronizer.cs" />