33
44static partial class Program
55{
6- static dynamic ? _word ;
7- static readonly ConsoleCtrlDelegate _consoleCtrlHandler = ConsoleCtrlHandler ;
8-
96 static int Main ( string [ ] args )
107 {
118 if ( args . Length != 2 )
@@ -47,12 +44,7 @@ static int Main(string[] args)
4744 } ;
4845 SetInformationJobObject ( job , JobObjectExtendedLimitInformation , ref info , ( uint ) Marshal . SizeOf ( info ) ) ;
4946
50- Console . CancelKeyPress += OnCancelKeyPress ;
51- AppDomain . CurrentDomain . ProcessExit += OnProcessExit ;
52- SetConsoleCtrlHandler ( _consoleCtrlHandler , true ) ;
53-
54- _word = Activator . CreateInstance ( wordType ) ! ;
55- dynamic word = _word ;
47+ dynamic word = Activator . CreateInstance ( wordType ) ! ;
5648
5749 // WdAlertLevel.wdAlertsNone = 0
5850 word . DisplayAlerts = 0 ;
@@ -102,44 +94,11 @@ static int Main(string[] args)
10294 // Release COM objects
10395 Marshal . ReleaseComObject ( comparedDoc ) ;
10496 Marshal . ReleaseComObject ( word ) ;
105- _word = null ;
10697 CloseHandle ( job ) ;
10798
10899 return 0 ;
109100 }
110101
111- static void OnCancelKeyPress ( object ? sender , ConsoleCancelEventArgs e ) =>
112- CloseWord ( ) ;
113-
114- static void OnProcessExit ( object ? sender , EventArgs e ) =>
115- CloseWord ( ) ;
116-
117- static bool ConsoleCtrlHandler ( int ctrlType )
118- {
119- CloseWord ( ) ;
120- return false ;
121- }
122-
123- static void CloseWord ( )
124- {
125- if ( _word == null )
126- {
127- return ;
128- }
129-
130- try
131- {
132- _word . Quit ( SaveChanges : false ) ;
133- Marshal . ReleaseComObject ( _word ) ;
134- }
135- catch
136- {
137- // Word may already be closed
138- }
139-
140- _word = null ;
141- }
142-
143102 const uint JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE = 0x2000 ;
144103 const int JobObjectExtendedLimitInformation = 9 ;
145104
@@ -182,10 +141,6 @@ struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
182141 [ LibraryImport ( "user32.dll" ) ]
183142 internal static partial uint GetWindowThreadProcessId ( IntPtr hWnd , out int processId ) ;
184143
185- [ LibraryImport ( "kernel32.dll" ) ]
186- [ return : MarshalAs ( UnmanagedType . Bool ) ]
187- private static partial bool SetConsoleCtrlHandler ( ConsoleCtrlDelegate handler , [ MarshalAs ( UnmanagedType . Bool ) ] bool add ) ;
188-
189144 [ LibraryImport ( "kernel32.dll" , EntryPoint = "CreateJobObjectW" , SetLastError = true , StringMarshalling = StringMarshalling . Utf16 ) ]
190145 private static partial IntPtr CreateJobObject ( IntPtr lpJobAttributes , string ? lpName ) ;
191146
@@ -200,6 +155,4 @@ struct JOBOBJECT_EXTENDED_LIMIT_INFORMATION
200155 [ LibraryImport ( "kernel32.dll" , SetLastError = true ) ]
201156 [ return : MarshalAs ( UnmanagedType . Bool ) ]
202157 private static partial bool CloseHandle ( IntPtr hObject ) ;
203-
204- delegate bool ConsoleCtrlDelegate ( int ctrlType ) ;
205158}
0 commit comments