public static Task SendTaskAsync(this SmtpClient smtpClient, MailMessage message) { return(AsyncCompatLibExtensions.SendTaskAsyncCore(smtpClient, message, delegate(TaskCompletionSource <object> tcs) { smtpClient.SendAsync(message, tcs); })); }
public static Task <PingReply> SendTaskAsync(this Ping ping, IPAddress address, int timeout, byte[] buffer) { return(AsyncCompatLibExtensions.SendTaskAsyncCore(ping, address, delegate(TaskCompletionSource <PingReply> tcs) { ping.SendAsync(address, timeout, buffer, tcs); })); }
public static Task <PingReply> SendTaskAsync(this Ping ping, string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options) { return(AsyncCompatLibExtensions.SendTaskAsyncCore(ping, hostNameOrAddress, delegate(TaskCompletionSource <PingReply> tcs) { ping.SendAsync(hostNameOrAddress, timeout, buffer, options, tcs); })); }
public static Task <ResolveResponse> ResolveTaskAsync(this DiscoveryClient discoveryClient, ResolveCriteria criteria) { if (discoveryClient == null) { throw new ArgumentNullException("discoveryClient"); } TaskCompletionSource <ResolveResponse> tcs = new TaskCompletionSource <ResolveResponse>(discoveryClient); EventHandler <ResolveCompletedEventArgs> completedHandler = null; completedHandler = delegate(object sender, ResolveCompletedEventArgs e) { AsyncCompatLibExtensions.HandleEapCompletion <ResolveResponse>(tcs, true, e, () => e.Result, delegate { discoveryClient.ResolveCompleted -= completedHandler; }); }; discoveryClient.ResolveCompleted += completedHandler; try { discoveryClient.ResolveAsync(criteria, tcs); } catch { discoveryClient.ResolveCompleted -= completedHandler; throw; } return(tcs.Task); }
public static Task <PingReply> SendTaskAsync(this Ping ping, string hostNameOrAddress) { return(AsyncCompatLibExtensions.SendTaskAsyncCore(ping, hostNameOrAddress, delegate(TaskCompletionSource <PingReply> tcs) { ping.SendAsync(hostNameOrAddress, tcs); })); }
private static Task SendTaskAsyncCore(SmtpClient smtpClient, object userToken, Action <TaskCompletionSource <object> > sendAsync) { if (smtpClient == null) { throw new ArgumentNullException("smtpClient"); } TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(userToken); SendCompletedEventHandler handler = null; handler = delegate(object sender, AsyncCompletedEventArgs e) { AsyncCompatLibExtensions.HandleEapCompletion <object>(tcs, true, e, () => null, delegate { smtpClient.SendCompleted -= handler; }); }; smtpClient.SendCompleted += handler; try { sendAsync(tcs); } catch { smtpClient.SendCompleted -= handler; throw; } return(tcs.Task); }
private static Task <PingReply> SendTaskAsyncCore(Ping ping, object userToken, Action <TaskCompletionSource <PingReply> > sendAsync) { if (ping == null) { throw new ArgumentNullException("ping"); } TaskCompletionSource <PingReply> tcs = new TaskCompletionSource <PingReply>(userToken); PingCompletedEventHandler handler = null; handler = delegate(object sender, PingCompletedEventArgs e) { AsyncCompatLibExtensions.HandleEapCompletion <PingReply>(tcs, true, e, () => e.Reply, delegate { ping.PingCompleted -= handler; }); }; ping.PingCompleted += handler; try { sendAsync(tcs); } catch { ping.PingCompleted -= handler; throw; } return(tcs.Task); }
public static Task WriteAsync(this Stream source, byte[] buffer, int offset, int count, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(AsyncCompatLibExtensions.FromCancellation(cancellationToken)); } return(Task.Factory.FromAsync <byte[], int, int>(new Func <byte[], int, int, AsyncCallback, object, IAsyncResult>(source.BeginWrite), new Action <IAsyncResult>(source.EndWrite), buffer, offset, count, null)); }
public static Task <XmlReader> ExecuteXmlReaderAsync(this SqlCommand source, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(AsyncCompatLibExtensions.FromCancellation <XmlReader>(cancellationToken)); } return(Task <XmlReader> .Factory.FromAsync(new Func <AsyncCallback, object, IAsyncResult>(source.BeginExecuteXmlReader), new Func <IAsyncResult, XmlReader>(source.EndExecuteXmlReader), null)); }
public static Task FlushAsync(this Stream source, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return(AsyncCompatLibExtensions.FromCancellation(cancellationToken)); } return(Task.Factory.StartNew(delegate(object s) { ((Stream)s).Flush(); }, source, cancellationToken, TaskCreationOptions.None, TaskScheduler.Default)); }
public static Task <string> UploadStringTaskAsync(this WebClient webClient, Uri address, string method, string data) { TaskCompletionSource <string> tcs = new TaskCompletionSource <string>(address); UploadStringCompletedEventHandler handler = null; handler = delegate(object sender, UploadStringCompletedEventArgs e) { AsyncCompatLibExtensions.HandleEapCompletion <string>(tcs, true, e, () => e.Result, delegate { webClient.UploadStringCompleted -= handler; }); }; webClient.UploadStringCompleted += handler; try { webClient.UploadStringAsync(address, method, data, tcs); } catch { webClient.UploadStringCompleted -= handler; throw; } return(tcs.Task); }
public static Task <Stream> OpenWriteTaskAsync(this WebClient webClient, Uri address, string method) { TaskCompletionSource <Stream> tcs = new TaskCompletionSource <Stream>(address); OpenWriteCompletedEventHandler handler = null; handler = delegate(object sender, OpenWriteCompletedEventArgs e) { AsyncCompatLibExtensions.HandleEapCompletion <Stream>(tcs, true, e, () => e.Result, delegate { webClient.OpenWriteCompleted -= handler; }); }; webClient.OpenWriteCompleted += handler; try { webClient.OpenWriteAsync(address, method, tcs); } catch { webClient.OpenWriteCompleted -= handler; throw; } return(tcs.Task); }
public static Task DownloadFileTaskAsync(this WebClient webClient, Uri address, string fileName) { TaskCompletionSource <object> tcs = new TaskCompletionSource <object>(address); AsyncCompletedEventHandler completedHandler = null; completedHandler = delegate(object sender, AsyncCompletedEventArgs e) { AsyncCompatLibExtensions.HandleEapCompletion <object>(tcs, true, e, () => null, delegate { webClient.DownloadFileCompleted -= completedHandler; }); }; webClient.DownloadFileCompleted += completedHandler; try { webClient.DownloadFileAsync(address, fileName, tcs); } catch { webClient.DownloadFileCompleted -= completedHandler; throw; } return(tcs.Task); }
public static Task <byte[]> DownloadDataTaskAsync(this WebClient webClient, Uri address) { TaskCompletionSource <byte[]> tcs = new TaskCompletionSource <byte[]>(address); DownloadDataCompletedEventHandler completedHandler = null; completedHandler = delegate(object sender, DownloadDataCompletedEventArgs e) { AsyncCompatLibExtensions.HandleEapCompletion <byte[]>(tcs, true, e, () => e.Result, delegate { webClient.DownloadDataCompleted -= completedHandler; }); }; webClient.DownloadDataCompleted += completedHandler; try { webClient.DownloadDataAsync(address, tcs); } catch { webClient.DownloadDataCompleted -= completedHandler; throw; } return(tcs.Task); }