@@ -25,7 +25,7 @@ public static class Server
2525 private static int _domesInUse ; // Keeps a count on the total number of domes alive.
2626 private static int _focusersInUse ; // Keeps a count on the total number of focusers alive.
2727 private static int _serverLocks ; // Keeps a lock count on this application.
28- private static List < Type > _driverTypes ; // Served COM object types
28+ private static List < Type > _driverTypes ; // Served COM object types
2929 private static List < ClassFactory > _classFactories ; // Served COM object class factories
3030 private static readonly string _appId = "{4f90ea04-044f-444e-963e-b52db2a87575}" ; // Our AppId
3131 private static readonly Object _lockObject = new object ( ) ;
@@ -34,7 +34,7 @@ public static class Server
3434
3535 private static TraceLogger Logger { get ; set ; }
3636
37- private static GarbageCollection GarbageCollector { get ; set ; }
37+ private static GarbageCollection GarbageCollector { get ; set ; }
3838
3939 private static MainWindow MainWindow { get ; set ; } // Reference to the main view
4040 private static MainWindowViewModel ViewModel { get ; set ; } // Reference to the main view model
@@ -157,72 +157,84 @@ internal static void Startup( string[] args )
157157
158158 ServiceInjector . InjectUIServices ( MainWindow ) ;
159159
160- // Create the ViewModel
160+ try
161+ {
162+ // Create the ViewModel
163+ // Any errors starting the view models or device managers will raise an exception and short circuit
164+ // the rest of the startup.
161165
162- ViewModel = new MainWindowViewModel ( ) ;
166+ ViewModel = new MainWindowViewModel ( ) ;
163167
164- Logger . LogMessage ( logId , "Setting the data context for the main view" ) ;
168+ Logger . LogMessage ( logId , "Setting the data context for the main view" ) ;
165169
166- MainWindow . DataContext = ViewModel ;
167- MainWindow . Closing += MainWindow_Closing ;
170+ MainWindow . DataContext = ViewModel ;
171+ MainWindow . Closing += MainWindow_Closing ;
168172
169- // Load the saved settings to ensure that everyone up-to-date. Be sure to do this
170- // after the main window is created so we can set its location.
173+ // Load the saved settings to ensure that everyone up-to-date. Be sure to do this
174+ // after the main window is created so we can set its location.
171175
172- Logger . LogMessage ( logId , "Loading the application settings" ) ;
176+ Logger . LogMessage ( logId , "Loading the application settings" ) ;
173177
174- AppSettingsManager . LoadAppSettings ( ) ;
178+ AppSettingsManager . LoadAppSettings ( ) ;
175179
176- Logger . LogMessage ( logId , "Loading the device driver settings" ) ;
180+ Logger . LogMessage ( logId , "Loading the device driver settings" ) ;
177181
178- LoadDeviceSettings ( ) ;
182+ LoadDeviceSettings ( ) ;
179183
180- // Register the class factories of the served objects
184+ // Register the class factories of the served objects
181185
182- Logger . LogMessage ( logId , "Registering class factories" ) ;
186+ Logger . LogMessage ( logId , "Registering class factories" ) ;
183187
184- RegisterClassFactories ( ) ;
188+ RegisterClassFactories ( ) ;
185189
186- Logger . LogMessage ( logId , "Starting garbage collection" ) ;
190+ Logger . LogMessage ( logId , "Starting garbage collection" ) ;
187191
188- StartGarbageCollection ( 60000 ) ; // Collect garbage once a minute.
189-
190- try
191- {
192- Logger . LogMessage ( logId , "Starting main view" ) ;
192+ StartGarbageCollection ( 60000 ) ; // Collect garbage once a minute.
193193
194- ShowMainWindow ( ) ;
194+ try
195+ {
196+ Logger . LogMessage ( logId , "Starting main view" ) ;
195197
196- Logger . LogMessage ( logId , "The main view has closed" ) ;
197- }
198- finally
199- {
200- Logger . LogMessage ( logId , "Saving the application settings" ) ;
201- AppSettingsManager . SaveAppSettings ( ) ;
198+ ShowMainWindow ( ) ;
202199
203- // Revoke the class factories immediately.
204- // Don't wait until the thread has stopped before
205- // we perform revocation!!!
200+ Logger . LogMessage ( logId , "The main view has closed" ) ;
201+ }
202+ finally
203+ {
204+ Logger . LogMessage ( logId , "Saving the application settings" ) ;
205+ AppSettingsManager . SaveAppSettings ( ) ;
206206
207- RevokeClassFactories ( ) ;
207+ // Revoke the class factories immediately.
208+ // Don't wait until the thread has stopped before
209+ // we perform revocation!!!
208210
209- Logger . LogMessage ( logId , "Disposing the main view and viewmodel." ) ;
211+ RevokeClassFactories ( ) ;
210212
211- MainWindow . DataContext = null ;
212- MainWindow = null ;
213- ViewModel . Dispose ( ) ;
214- ViewModel = null ;
213+ Logger . LogMessage ( logId , "Disposing the main view and viewmodel." ) ;
215214
216- Logger . LogMessage ( logId , "Unregistering all services" ) ;
215+ MainWindow . DataContext = null ;
216+ MainWindow = null ;
217+ ViewModel . Dispose ( ) ;
218+ ViewModel = null ;
217219
218- ServiceContainer . Instance . ClearAllServices ( ) ;
220+ Logger . LogMessage ( logId , "Unregistering all services" ) ;
219221
220- // Now stop the Garbage Collector task.
222+ ServiceContainer . Instance . ClearAllServices ( ) ;
221223
222- Logger . LogMessage ( logId , "Stopping garbage collection" ) ;
224+ // Now stop the Garbage Collector task.
223225
224- StopGarbageCollection ( ) ;
226+ Logger . LogMessage ( logId , "Stopping garbage collection" ) ;
225227
228+ StopGarbageCollection ( ) ;
229+ }
230+ }
231+ catch ( Exception )
232+ {
233+ // Any exception from starting the view models or device managers will bring us here.
234+ // The exception was already logged via the AppLogger so we have nothing more to do but close the Logger and return.
235+ }
236+ finally
237+ {
226238 Logger . LogMessage ( logId , "Local server is shutting down" ) ;
227239 Logger . Dispose ( ) ;
228240 }
0 commit comments