@@ -74,10 +74,10 @@ public override void Flush()
7474            } 
7575
7676            // Get how many characters are in the Notepad editor already 
77-             var  textLength  =  User32 . SendMessage ( _currentNotepadEditorHandle ,  User32 . WM_GETTEXTLENGTH ,  IntPtr . Zero ,  IntPtr . Zero ) ; 
77+             var  textLengthBefore  =  User32 . SendMessage ( _currentNotepadEditorHandle ,  User32 . WM_GETTEXTLENGTH ,  IntPtr . Zero ,  IntPtr . Zero ) ; 
7878
7979            // Set the caret position to the end of the text 
80-             User32 . SendMessage ( _currentNotepadEditorHandle ,  User32 . EM_SETSEL ,  ( IntPtr ) textLength ,  ( IntPtr ) textLength ) ; 
80+             User32 . SendMessage ( _currentNotepadEditorHandle ,  User32 . EM_SETSEL ,  ( IntPtr ) textLengthBefore ,  ( IntPtr ) textLengthBefore ) ; 
8181
8282            var  buffer  =  base . GetStringBuilder ( ) ; 
8383            var  message  =  buffer . ToString ( ) ; 
@@ -88,13 +88,17 @@ public override void Flush()
8888            // Get how many characters are in the Notepad editor after putting in new text 
8989            var  textLengthAfter  =  User32 . SendMessage ( _currentNotepadEditorHandle ,  User32 . WM_GETTEXTLENGTH ,  IntPtr . Zero ,  IntPtr . Zero ) ; 
9090
91-             // If no change in textLength, reset editor handle to try to find it again. 
92-             if  ( textLengthAfter  ==  textLength ) 
91+             // If no change in the text length, reset editor handle to try to find it again. 
92+             if  ( textLengthAfter  ==  textLengthBefore ) 
93+             { 
9394                _currentNotepadEditorHandle  =  IntPtr . Zero ; 
94- 
95-             // Otherwise, we clear the buffer 
95+             } 
9696            else 
97+             { 
98+                 // Otherwise, we clear the buffer 
99+ 
97100                buffer . Clear ( ) ; 
101+             } 
98102        } 
99103
100104        protected  override  void  Dispose ( bool  disposing ) 
@@ -161,9 +165,10 @@ private static string GetClassNameFromWindow(IntPtr handle)
161165
162166        private  static bool  EnumWindow ( IntPtr  handle ,  IntPtr  pointer ) 
163167        { 
164-             GCHandle  gch  =  GCHandle . FromIntPtr ( pointer ) ; 
165-             List < IntPtr >  list  =  gch . Target  as  List < IntPtr > ; 
166-             if  ( list  ==  null ) 
168+             var  gch  =  GCHandle . FromIntPtr ( pointer ) ; 
169+             var  list  =  gch . Target  as  List < IntPtr > ; 
170+ 
171+             if  ( list  is  null ) 
167172            { 
168173                throw  new  InvalidCastException ( "GCHandle Target could not be cast as List<IntPtr>" ) ; 
169174            } 
@@ -181,8 +186,9 @@ private static bool EnumWindow(IntPtr handle, IntPtr pointer)
181186
182187        private  static IntPtr  FindEditorHandleThroughChildWindows ( IntPtr  notepadWindowHandle ) 
183188        { 
184-             List < IntPtr >  result  =  new  List < IntPtr > ( 1 ) ; 
185-             GCHandle  listHandle  =  GCHandle . Alloc ( result ) ; 
189+             var  result  =  new  List < IntPtr > ( 1 ) ; 
190+             var  listHandle  =  GCHandle . Alloc ( result ) ; 
191+ 
186192            try 
187193            { 
188194                User32 . Win32Callback  childProc  =  new  User32 . Win32Callback ( EnumWindow ) ; 
@@ -191,8 +197,11 @@ private static IntPtr FindEditorHandleThroughChildWindows(IntPtr notepadWindowHa
191197            finally 
192198            { 
193199                if  ( listHandle . IsAllocated ) 
200+                 { 
194201                    listHandle . Free ( ) ; 
202+                 } 
195203            } 
204+ 
196205            return  result . FirstOrDefault ( ) ; 
197206        } 
198207    } 
0 commit comments