@@ -66,12 +66,16 @@ public static string Download(Uri src, string path)
6666 /// <param name="url">Url to search</param>
6767 /// <param name="count">Number of direct images to return</param>
6868 /// <param name="timeoutMS"></param>
69- public static async Task < List < DirectImage > > ScanForImages ( string url , int count = 10 , long timeoutMS = TimeoutMS )
69+ /// <param name="c"></param>
70+ public static async Task < List < DirectImage > > ScanForImages ( string url , int count = 10 , long timeoutMS = TimeoutMS ,
71+ CancellationToken ? c2 = null )
7072 {
7173 var images = new List < DirectImage > ( ) ;
7274
7375 IHtmlDocument document = null ;
7476
77+ var c = c2 ?? CancellationToken . None ;
78+
7579 try {
7680 var client = new HttpClient ( ) ;
7781 var task = client . GetStringAsync ( url ) ;
@@ -90,7 +94,6 @@ public static async Task<List<DirectImage>> ScanForImages(string url, int count
9094 return images ;
9195 }
9296
93- using var cts = new CancellationTokenSource ( ) ;
9497
9598 var urls = new List < string > ( ) ;
9699
@@ -132,15 +135,15 @@ public static async Task<List<DirectImage>> ScanForImages(string url, int count
132135 {
133136 string s = urls [ iCopy ] ;
134137
135- if ( IsImage ( s , ( int ) timeoutMS , out var di ) ) {
138+ if ( IsImage ( s , ( int ) timeoutMS , out var di , c ) ) {
136139 return di ;
137140 }
138141
139142 di ? . Dispose ( ) ;
140143
141144 return null ;
142145
143- } , cts . Token ) ) ;
146+ } , c ) ) ;
144147 }
145148
146149 while ( tasks . Any ( ) && count != 0 ) {
@@ -164,11 +167,11 @@ public static async Task<List<DirectImage>> ScanForImages(string url, int count
164167 return images ;
165168 }
166169
167- public static bool IsImage ( string url , out DirectImage di )
168- => IsImage ( url , TimeoutMS , out di ) ;
170+ public static bool IsImage ( string url , out DirectImage di , CancellationToken ? c = null )
171+ => IsImage ( url , TimeoutMS , out di , c ) ;
169172
170173
171- public static bool IsImage ( string url , long timeout , out DirectImage di )
174+ public static bool IsImage ( string url , long timeout , out DirectImage di , CancellationToken ? c = null )
172175 {
173176 di = new DirectImage ( ) ;
174177
@@ -177,7 +180,9 @@ public static bool IsImage(string url, long timeout, out DirectImage di)
177180 }
178181
179182
180- var response = HttpUtilities . GetHttpResponse ( url , ( int ) timeout , HttpMethod . Head ) ;
183+ var response1 = HttpUtilities . GetHttpResponseAsync ( url , ( int ) timeout , HttpMethod . Head , token : c ) ;
184+ response1 . Wait ( ) ;
185+ var response = response1 . Result ;
181186
182187 // var response1 = HttpUtilities.GetHttpResponseAsync(url, (int) timeout, HttpMethod.Head, c: c);
183188 // response1.Wait();
@@ -210,7 +215,7 @@ public static bool IsImage(string url, long timeout, out DirectImage di)
210215
211216 try {
212217 using var client = new HttpClient ( ) ;
213- var task = client . GetStreamAsync ( url ) ;
218+ var task = client . GetStreamAsync ( url , c ?? CancellationToken . None ) ;
214219 task . Wait ( ( int ) timeout ) ;
215220
216221 var stream = task . Result ;
0 commit comments