From f5ed784fe69d5b6b321d5e08d0ab10840b2baba2 Mon Sep 17 00:00:00 2001 From: mo khan Date: Mon, 19 Jul 2010 12:55:31 -0600 Subject: figured out why the application was not getting shutdown. needed to stop the command processor and the queue. --- product/client/client.ui/Program.cs | 23 ++- product/client/client.ui/WPFApplication.cs | 6 + .../client/client.ui/bootstrappers/Bootstrapper.cs | 3 +- product/client/client.ui/client.csproj | 1 + product/client/common/common.csproj | 1 + .../common/messages/ApplicationShuttingDown.cs | 9 + product/client/server/Bootstrapper.cs | 1 + .../server/handlers/ShutdownApplicationCommand.cs | 16 ++ product/client/server/server.csproj | 3 +- .../threading/AsynchronousCommandProcessor.cs | 212 ++++++++++----------- 10 files changed, 161 insertions(+), 114 deletions(-) create mode 100644 product/client/client.ui/WPFApplication.cs create mode 100644 product/client/common/messages/ApplicationShuttingDown.cs create mode 100644 product/client/server/handlers/ShutdownApplicationCommand.cs diff --git a/product/client/client.ui/Program.cs b/product/client/client.ui/Program.cs index 8c83db8..e2f20df 100644 --- a/product/client/client.ui/Program.cs +++ b/product/client/client.ui/Program.cs @@ -4,6 +4,7 @@ using System.IO; using System.Security.Principal; using System.Windows; using System.Windows.Threading; +using Gorilla.Commons.Infrastructure.Logging; using presentation.windows.bootstrappers; using presentation.windows.views; @@ -14,23 +15,33 @@ namespace presentation.windows [STAThread] static public void Main(string[] args) { - Process.Start(get_startup_path_using(args)); + Process.Start(new ProcessStartInfo + { + FileName = get_service_startup_path(args), + //WindowStyle = ProcessWindowStyle.Hidden, + }); + AppDomain.CurrentDomain.UnhandledException += (o, e) => + { + (e.ExceptionObject as Exception).add_to_log(); + }; AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); Dispatcher.CurrentDispatcher.UnhandledException += (o, e) => { + e.Exception.add_to_log(); new ErrorWindow {DataContext = e.Exception}.ShowDialog(); + e.Handled = true; }; - new Application + new WPFApplication { - ShutdownMode = ShutdownMode.OnMainWindowClose + ShutdownMode = ShutdownMode.OnMainWindowClose, }.Run(Bootstrapper.create_window()); } - static string get_startup_path_using(string[] args) + static string get_service_startup_path(string[] args) { if (args.Length > 0) - return Path.Combine(args[0], @"presentation.windows.server.exe"); - return Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\server\bin\Debug\presentation.windows.server.exe")); + return Path.Combine(args[0], @"momoney.server.exe"); + return Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, @"..\..\..\server\bin\Debug\momoney.server.exe")); } } } \ No newline at end of file diff --git a/product/client/client.ui/WPFApplication.cs b/product/client/client.ui/WPFApplication.cs new file mode 100644 index 0000000..3005e44 --- /dev/null +++ b/product/client/client.ui/WPFApplication.cs @@ -0,0 +1,6 @@ +using System.Windows; + +namespace presentation.windows +{ + public class WPFApplication : Application {} +} \ No newline at end of file diff --git a/product/client/client.ui/bootstrappers/Bootstrapper.cs b/product/client/client.ui/bootstrappers/Bootstrapper.cs index 488914c..c33d308 100644 --- a/product/client/client.ui/bootstrappers/Bootstrapper.cs +++ b/product/client/client.ui/bootstrappers/Bootstrapper.cs @@ -71,13 +71,14 @@ namespace presentation.windows.bootstrappers builder.Register().As(); // queries - builder.Register>().As(); Resolve.the>().each(x => x.run()); Resolve.the().run(); + shell_window.Closed += (o, e) => Resolve.the().publish(); shell_window.Closed += (o, e) => Resolve.the().stop(); + shell_window.Closed += (o, e) => manager.Dispose(); return shell_window; } } diff --git a/product/client/client.ui/client.csproj b/product/client/client.ui/client.csproj index 7501df4..8f77774 100644 --- a/product/client/client.ui/client.csproj +++ b/product/client/client.ui/client.csproj @@ -212,6 +212,7 @@ StatusBarRegion.xaml + diff --git a/product/client/common/common.csproj b/product/client/common/common.csproj index 3321e26..801ac68 100644 --- a/product/client/common/common.csproj +++ b/product/client/common/common.csproj @@ -87,6 +87,7 @@ + diff --git a/product/client/common/messages/ApplicationShuttingDown.cs b/product/client/common/messages/ApplicationShuttingDown.cs new file mode 100644 index 0000000..e920e33 --- /dev/null +++ b/product/client/common/messages/ApplicationShuttingDown.cs @@ -0,0 +1,9 @@ +using System; +using ProtoBuf; + +namespace presentation.windows.common.messages +{ + [Serializable] + [ProtoContract] + public class ApplicationShuttingDown : IEvent {} +} \ No newline at end of file diff --git a/product/client/server/Bootstrapper.cs b/product/client/server/Bootstrapper.cs index 0896466..254837c 100644 --- a/product/client/server/Bootstrapper.cs +++ b/product/client/server/Bootstrapper.cs @@ -55,6 +55,7 @@ namespace presentation.windows.server builder.Register().As(); builder.Register().As(); builder.Register().As(); + builder.Register().As(); // queries diff --git a/product/client/server/handlers/ShutdownApplicationCommand.cs b/product/client/server/handlers/ShutdownApplicationCommand.cs new file mode 100644 index 0000000..dd78c48 --- /dev/null +++ b/product/client/server/handlers/ShutdownApplicationCommand.cs @@ -0,0 +1,16 @@ +using System; +using Gorilla.Commons.Infrastructure.Logging; +using presentation.windows.common; +using presentation.windows.common.messages; + +namespace presentation.windows.server.handlers +{ + public class ShutdownApplicationCommand : AbstractHandler + { + public override void handle(ApplicationShuttingDown item) + { + this.log().debug("shutting down"); + Environment.Exit(Environment.ExitCode); + } + } +} \ No newline at end of file diff --git a/product/client/server/server.csproj b/product/client/server/server.csproj index c952b80..804074a 100644 --- a/product/client/server/server.csproj +++ b/product/client/server/server.csproj @@ -9,7 +9,7 @@ Exe Properties presentation.windows.server - presentation.windows.server + momoney.server v4.0 512 @@ -128,6 +128,7 @@ + diff --git a/product/commons/infrastructure/threading/AsynchronousCommandProcessor.cs b/product/commons/infrastructure/threading/AsynchronousCommandProcessor.cs index 7bfae67..4505abc 100644 --- a/product/commons/infrastructure/threading/AsynchronousCommandProcessor.cs +++ b/product/commons/infrastructure/threading/AsynchronousCommandProcessor.cs @@ -4,110 +4,110 @@ using System.Threading; using Gorilla.Commons.Infrastructure.Logging; using gorilla.commons.utility; -namespace gorilla.commons.infrastructure.threading -{ - public class AsynchronousCommandProcessor : CommandProcessor - { - readonly Queue queued_commands; - readonly EventWaitHandle manual_reset; - readonly IList worker_threads; - bool keep_working; - - static public readonly Command Empty = new EmptyCommand(); - - public AsynchronousCommandProcessor() - { - queued_commands = new Queue(); - worker_threads = new List(); - manual_reset = new ManualResetEvent(false); - } - - public void add(Action command) - { - add(new AnonymousCommand(command)); - } - - public void add(Command command_to_process) - { - lock (queued_commands) - { - if (queued_commands.Contains(command_to_process)) return; - queued_commands.Enqueue(command_to_process); - reset_thread(); - } - } - - public void run() - { - reset_thread(); - keep_working = true; - var worker_thread = new Thread(run_commands); - worker_thread.SetApartmentState(ApartmentState.STA); - worker_threads.Add(worker_thread); - worker_thread.Start(); - } - - public void stop() - { - keep_working = false; - manual_reset.Set(); - //manual_reset.Close(); - } - - [STAThread] - void run_commands() - { - while (keep_working) - { - manual_reset.WaitOne(); - run_next_command(); - } - } - - void run_next_command() - { - var command = Empty; - within_lock(() => - { - if (queued_commands.Count == 0) - manual_reset.Reset(); - else - command = queued_commands.Dequeue(); - }); - safely_invoke(() => - { - command.run(); - }); - reset_thread(); - } - - void safely_invoke(Action action) - { - try - { - action(); - } - catch (Exception e) - { - this.log().error(e); - } - } - - void reset_thread() - { - within_lock(() => - { - if (queued_commands.Count > 0) manual_reset.Set(); - else manual_reset.Reset(); - }); - } - - void within_lock(Action action) - { - lock (queued_commands) - { - action(); - } - } - } +namespace gorilla.commons.infrastructure.threading +{ + public class AsynchronousCommandProcessor : CommandProcessor + { + readonly Queue queued_commands; + readonly EventWaitHandle manual_reset; + readonly IList worker_threads; + bool keep_working; + + static public readonly Command Empty = new EmptyCommand(); + + public AsynchronousCommandProcessor() + { + queued_commands = new Queue(); + worker_threads = new List(); + manual_reset = new ManualResetEvent(false); + } + + public void add(Action command) + { + add(new AnonymousCommand(command)); + } + + public void add(Command command_to_process) + { + lock (queued_commands) + { + if (queued_commands.Contains(command_to_process)) return; + queued_commands.Enqueue(command_to_process); + reset_thread(); + } + } + + public void run() + { + reset_thread(); + keep_working = true; + var worker_thread = new Thread(run_commands); + worker_thread.SetApartmentState(ApartmentState.STA); + worker_threads.Add(worker_thread); + worker_thread.Start(); + } + + public void stop() + { + keep_working = false; + manual_reset.Set(); + //manual_reset.Close(); + } + + [STAThread] + void run_commands() + { + while (keep_working) + { + manual_reset.WaitOne(); + run_next_command(); + } + } + + void run_next_command() + { + var command = Empty; + within_lock(() => + { + if (queued_commands.Count == 0) + manual_reset.Reset(); + else + command = queued_commands.Dequeue(); + }); + safely_invoke(() => + { + command.run(); + }); + reset_thread(); + } + + void safely_invoke(Action action) + { + try + { + action(); + } + catch (Exception e) + { + this.log().error(e); + } + } + + void reset_thread() + { + within_lock(() => + { + if (queued_commands.Count > 0) manual_reset.Set(); + else manual_reset.Reset(); + }); + } + + void within_lock(Action action) + { + lock (queued_commands) + { + action(); + } + } + } } \ No newline at end of file -- cgit v1.2.3