string data2 = UploadString ((Uri) args [0], (string) args [1], (string) args [2]);
OnUploadStringCompleted (
new UploadStringCompletedEventArgs (data2, null, false, args [3]));
- } catch (ThreadInterruptedException){
- OnUploadStringCompleted (
- new UploadStringCompletedEventArgs (null, null, true, args [3]));
} catch (Exception e){
+ if (e is ThreadInterruptedException || e.InnerException is ThreadInterruptedException) {
+ OnUploadStringCompleted (
+ new UploadStringCompletedEventArgs (null, null, true, args [3]));
+ return;
+ }
OnUploadStringCompleted (
new UploadStringCompletedEventArgs (null, e, false, args [3]));
}});
// and return the same instance as WebRequest.DefaultWebProxy
Assert.AreSame (wc.Proxy, WebRequest.DefaultWebProxy);
}
+
+ [Test]
+ public void UploadStringAsyncCancelEvent ()
+ {
+ UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+ {
+
+ webClient.UploadStringCompleted += (sender, args) =>
+ {
+ if (args.Cancelled)
+ cancelEvent.Set ();
+ };
+
+ webClient.UploadStringAsync (uri, "PUT", "text");
+ });
+ }
+
+ [Test]
+ public void UploadDataAsyncCancelEvent ()
+ {
+ UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+ {
+ webClient.UploadDataCompleted += (sender, args) =>
+ {
+ if (args.Cancelled)
+ cancelEvent.Set ();
+ };
+
+ webClient.UploadDataAsync (uri, "PUT", new byte[] { });
+ });
+ }
+
+ [Test]
+ public void UploadValuesAsyncCancelEvent ()
+ {
+ UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+ {
+ webClient.UploadValuesCompleted += (sender, args) =>
+ {
+ if (args.Cancelled)
+ cancelEvent.Set ();
+ };
+
+ webClient.UploadValuesAsync (uri, "PUT", new NameValueCollection ());
+ });
+ }
+
+ [Test]
+ public void UploadFileAsyncCancelEvent ()
+ {
+ UploadAsyncCancelEventTest ((webClient, uri, cancelEvent) =>
+ {
+ string tempFile = Path.Combine (_tempFolder, "upload.tmp");
+ File.Create (tempFile).Close ();
+
+ webClient.UploadFileCompleted += (sender, args) =>
+ {
+ if (args.Cancelled)
+ cancelEvent.Set ();
+ };
+
+ webClient.UploadFileAsync (uri, "PUT", tempFile);
+ });
+ }
+
+ public void UploadAsyncCancelEventTest (Action<WebClient, Uri, EventWaitHandle> uploadAction)
+ {
+ var ep = new IPEndPoint (IPAddress.Loopback, 8000);
+ string url = "http://" + IPAddress.Loopback + ":8000/test/";
+
+ using (var responder = new SocketResponder (ep, EchoRequestHandler))
+ {
+ responder.Start ();
+
+ var webClient = new WebClient ();
+
+ var cancellationTokenSource = new CancellationTokenSource ();
+ cancellationTokenSource.Token.Register (webClient.CancelAsync);
+
+ var cancelEvent = new ManualResetEvent (false);
+
+ uploadAction.Invoke (webClient, new Uri (url), cancelEvent);
+
+ cancellationTokenSource.Cancel ();
+
+ Assert.IsTrue (cancelEvent.WaitOne (1000));
+ }
+ }
}
}