Commit b1d9d1c
Changed files (4)
trunk
product
MyMoney
Infrastructure
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" />