@@ -15,53 +15,39 @@ static class Program
1515 static int Main ( string [ ] args )
1616 {
1717
18+ AppDomain . CurrentDomain . UnhandledException += CurrentDomainUnhandledException ;
19+
1820 if ( Environment . UserInteractive )
1921 {
20- string parameter = string . Concat ( args ) ;
21- try
22+ switch ( string . Concat ( args ) )
2223 {
23- switch ( parameter )
24- {
25- case "--install" :
26- ManagedInstallerClass . InstallHelper ( new string [ ] { Assembly . GetExecutingAssembly ( ) . Location } ) ;
27- Console . WriteLine ( "Service Installed" ) ;
28- const string serviceName = "NetworkAutoSwitch" ;
29- int timeout = 5000 ;
30- Console . WriteLine ( String . Format ( "Starting Windows Service {0} with timeout of {1} ms" , serviceName , timeout ) ) ;
31- Console . WriteLine ( "Service running" ) ;
32- StartService ( serviceName , timeout ) ;
33- return 0 ;
34- case "--uninstall" :
35- ManagedInstallerClass . InstallHelper ( new string [ ] { "/u" , Assembly . GetExecutingAssembly ( ) . Location } ) ;
36- Console . WriteLine ( "Service Uninstalled" ) ;
37- return 0 ;
38- default :
39- Console . WriteLine ( "Use parameters --install or --uninstall to use as Windows Service" ) ;
40- var exitEvent = new ManualResetEvent ( false ) ;
41- Console . CancelKeyPress += ( sender , eventArgs ) => {
42- eventArgs . Cancel = true ;
43- exitEvent . Set ( ) ;
44- } ;
45- try
46- {
47- DetectNetworkChanges detectNetworkChanges = new DetectNetworkChanges ( ) ;
48- Console . WriteLine ( "Press CTRL + C to exit..." ) ;
49- exitEvent . WaitOne ( ) ;
50- detectNetworkChanges . StopNow ( ) ;
51- return 0 ;
52- }
53- catch ( Exception ex )
54- {
55- Logging . WriteMessage ( ex . ToString ( ) ) ;
56- return 1 ;
57- }
24+ case "--install" :
25+ ManagedInstallerClass . InstallHelper ( new string [ ] { Assembly . GetExecutingAssembly ( ) . Location } ) ;
26+ Console . WriteLine ( "Service Installed" ) ;
27+ const string serviceName = "NetworkAutoSwitch" ;
28+ int timeout = 5000 ;
29+ Console . WriteLine ( String . Format ( "Starting Windows Service {0} with timeout of {1} ms" , serviceName , timeout ) ) ;
30+ Console . WriteLine ( "Service running" ) ;
31+ StartService ( serviceName , timeout ) ;
32+ return 0 ;
33+ case "--uninstall" :
34+ ManagedInstallerClass . InstallHelper ( new string [ ] { "/u" , Assembly . GetExecutingAssembly ( ) . Location } ) ;
35+ Console . WriteLine ( "Service Uninstalled" ) ;
36+ return 0 ;
37+ default :
38+ Console . WriteLine ( "Use parameters --install or --uninstall to use as Windows Service" ) ;
39+ var exitEvent = new ManualResetEvent ( false ) ;
40+ Console . CancelKeyPress += ( sender , eventArgs ) => {
41+ eventArgs . Cancel = true ;
42+ exitEvent . Set ( ) ;
43+ } ;
44+
45+ DetectNetworkChanges detectNetworkChanges = new DetectNetworkChanges ( ) ;
46+ Console . WriteLine ( "Press CTRL + C to exit..." ) ;
47+ exitEvent . WaitOne ( ) ;
48+ detectNetworkChanges . StopNow ( ) ;
49+ return 0 ;
5850
59- }
60- }
61- catch ( Exception ex )
62- {
63- Console . WriteLine ( "Error: " + ex . Message ) ;
64- return 1 ;
6551 }
6652 }
6753 else
@@ -77,6 +63,13 @@ static int Main(string[] args)
7763
7864 }
7965
66+ private static void CurrentDomainUnhandledException ( object sender , UnhandledExceptionEventArgs e )
67+ {
68+ Logging . WriteMessage ( e . ExceptionObject . ToString ( ) ) ;
69+ Environment . Exit ( 1 ) ;
70+ }
71+
72+
8073 private static void StartService ( string serviceName , int timeoutMilliseconds )
8174 {
8275 ServiceController service = new ServiceController ( serviceName ) ;
0 commit comments