by
0
5
85k
2
Top 1% !
Pearl of Wisdom
Easy-to-find
Specified
OpenSource
Popularity: 28994th place

Published on:

LanguageCSharp
LicenseMIT_X11
OSWindows

SendTaskCore: The core implementation of SendTask.

<summary>The core implementation of SendTask.</summary>
<param name="smtpClient">The client.</param>
<param name="userToken">The user-supplied state.</param>
<param name="sendAsync">
A delegate that initiates the asynchronous send.
The provided TaskCompletionSource must be passed as the user-supplied state to the actual SmtpClient.SendAsync method.
</param>
<returns></returns>
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/291548/SendTaskCore--The-core-implementation-of?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
using System.Net.Mail; using System.Threading.Tasks; /// <summary>The core implementation of SendTask.</summary> /// <param name="smtpClient">The client.</param> /// <param name="userToken">The user-supplied state.</param> /// <param name="sendAsync"> /// A delegate that initiates the asynchronous send. /// The provided TaskCompletionSource must be passed as the user-supplied state to the actual SmtpClient.SendAsync method. /// </param> /// <returns></returns> private static Task SendTaskCore(SmtpClient smtpClient, object userToken, Action<TaskCompletionSource<object>> sendAsync) { // Validate we're being used with a real smtpClient. The rest of the arg validation // will happen in the call to sendAsync. if (smtpClient == null) throw new ArgumentNullException("smtpClient"); // Create a TaskCompletionSource to represent the operation var tcs = new TaskCompletionSource<object>(userToken); // Register a handler that will transfer completion results to the TCS Task SendCompletedEventHandler handler = null; handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => null, () => smtpClient.SendCompleted -= handler); smtpClient.SendCompleted += handler; // Try to start the async operation. If starting it fails (due to parameter validation) // unregister the handler before allowing the exception to propagate. try { sendAsync(tcs); } catch(Exception exc) { smtpClient.SendCompleted -= handler; tcs.TrySetException(exc); } // Return the task to represent the asynchronous operation return tcs.Task; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus