@@ -43,7 +43,7 @@ private static StreamReader AsyncStreamReader(string path, Encoding encoding)
4343 public static Task < string > ReadAllTextAsync ( string path , CancellationToken cancellationToken = default ( CancellationToken ) )
4444 => ReadAllTextAsync ( path , UTF8NoBOM , cancellationToken ) ;
4545
46- public static Task < string > ReadAllTextAsync ( string path , Encoding encoding , CancellationToken cancellationToken = default ( CancellationToken ) )
46+ public static async Task < string > ReadAllTextAsync ( string path , Encoding encoding , CancellationToken cancellationToken = default ( CancellationToken ) )
4747 {
4848 if ( path == null )
4949 throw new ArgumentNullException ( nameof ( path ) ) ;
@@ -52,9 +52,32 @@ private static StreamReader AsyncStreamReader(string path, Encoding encoding)
5252 if ( path . Length == 0 )
5353 throw new ArgumentException ( "SR.Argument_EmptyPath: {0}" , nameof ( path ) ) ;
5454
55- return cancellationToken . IsCancellationRequested
56- ? Task . FromCanceled < string > ( cancellationToken )
57- : InternalReadAllTextAsync ( path , encoding , cancellationToken ) ;
55+ while ( true ) //roland
56+ {
57+ try //roland
58+ {
59+ return cancellationToken . IsCancellationRequested
60+ ? await Task . FromCanceled < string > ( cancellationToken )
61+ : await InternalReadAllTextAsync ( path , encoding , cancellationToken ) ;
62+ }
63+ catch ( IOException ) //roland
64+ {
65+ //retry after delay
66+ try
67+ {
68+ #if ! NOASYNC
69+ await Task . Delay ( 1000 , cancellationToken ) ; //TODO: config file?
70+ #else
71+ cancellationToken . WaitHandle . WaitOne ( 1000 ) ;
72+ #endif
73+ }
74+ catch ( TaskCanceledException )
75+ {
76+ //do nothing here
77+ return await Task . FromCanceled < string > ( cancellationToken ) ;
78+ }
79+ }
80+ }
5881 }
5982
6083 private static async Task < string > InternalReadAllTextAsync ( string path , Encoding encoding , CancellationToken cancellationToken )
@@ -112,7 +135,7 @@ private static StreamWriter AsyncStreamWriter(string path, Encoding encoding, bo
112135 public static Task WriteAllTextAsync ( string path , string contents , CancellationToken cancellationToken = default ( CancellationToken ) )
113136 => WriteAllTextAsync ( path , contents , UTF8NoBOM , cancellationToken ) ;
114137
115- public static Task WriteAllTextAsync ( string path , string contents , Encoding encoding , CancellationToken cancellationToken = default ( CancellationToken ) )
138+ public static async Task WriteAllTextAsync ( string path , string contents , Encoding encoding , CancellationToken cancellationToken = default ( CancellationToken ) )
116139 {
117140 if ( path == null )
118141 throw new ArgumentNullException ( nameof ( path ) ) ;
@@ -121,18 +144,35 @@ private static StreamWriter AsyncStreamWriter(string path, Encoding encoding, bo
121144 if ( path . Length == 0 )
122145 throw new ArgumentException ( "SR.Argument_EmptyPath: {0}" , nameof ( path ) ) ;
123146
124- if ( cancellationToken . IsCancellationRequested )
147+ while ( true ) //roland
125148 {
126- return Task . FromCanceled ( cancellationToken ) ;
127- }
149+ try //roland
150+ {
151+ cancellationToken . ThrowIfCancellationRequested ( ) ;
152+ //if (cancellationToken.IsCancellationRequested)
153+ //{
154+ // return Task.FromCanceled(cancellationToken);
155+ //}
128156
129- if ( string . IsNullOrEmpty ( contents ) )
130- {
131- new FileStream ( path , FileMode . Create , FileAccess . Write , FileShare . Read ) . Dispose ( ) ;
132- return Task . CompletedTask ;
133- }
157+ if ( string . IsNullOrEmpty ( contents ) )
158+ {
159+ new FileStream ( path , FileMode . Create , FileAccess . Write , FileShare . Read ) . Dispose ( ) ;
160+ return ; // await Task.CompletedTask;
161+ }
134162
135- return InternalWriteAllTextAsync ( AsyncStreamWriter ( path , encoding , append : false ) , contents , cancellationToken ) ;
163+ await InternalWriteAllTextAsync ( AsyncStreamWriter ( path , encoding , append : false ) , contents , cancellationToken ) ;
164+ return ;
165+ }
166+ catch ( IOException ) //roland
167+ {
168+ //retry after delay
169+ #if ! NOASYNC
170+ await Task . Delay ( 1000 , cancellationToken ) ; //TODO: config file?
171+ #else
172+ cancellationToken . WaitHandle . WaitOne ( 1000 ) ;
173+ #endif
174+ }
175+ }
136176 }
137177
138178 private static async Task InternalWriteAllTextAsync ( StreamWriter sw , string contents , CancellationToken cancellationToken )
0 commit comments