public void EndSend(IAsyncResult asyncResult)
        {
            var smtpResult = asyncResult as SmtpWorkerAsyncResult;

            if (smtpResult == null)
            {
                throw new ArgumentException("IAsyncResult is of the wrong type.", "asyncResult");
            }
            SmtpWorkerAsyncResult.End(smtpResult);
        }
 public IAsyncResult BeginSend(MailMessage message, AsyncCallback callback, object state)
 {
     var asyncResult = new SmtpWorkerAsyncResult(message, callback, state);
     lock (queueLock)
     {
         // lock keeps queue count and active client count in sync
         messageQueue.Enqueue(asyncResult);
         EnsureWorker();
     }
     return asyncResult;
 }
Esempio n. 3
0
        public IAsyncResult BeginSend(MailMessage message, AsyncCallback callback, object state)
        {
            var asyncResult = new SmtpWorkerAsyncResult(message, callback, state);

            lock (queueLock)
            {
                // lock keeps queue count and active client count in sync
                messageQueue.Enqueue(asyncResult);
                EnsureWorker();
            }
            return(asyncResult);
        }
        private void SendCompleted(object sender, AsyncCompletedEventArgs e)
        {
            SmtpWorkerAsyncResult completedAsyncResult = (SmtpWorkerAsyncResult)e.UserState;

            if (e.Error != null)
            {
                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "SendCompleted Exception: {0}", e.Error));
            }
            completedAsyncResult.Complete(e.Error, false);
            completedAsyncResult.Dispose();
            // Use the completed thread to check for any new messages in queue
            var smtpClient = (SmtpClient)sender;

            ProcessQueue(smtpClient);
        }
 public IAsyncResult BeginSend(MailMessage message, AsyncCallback callback, object state)
 {
     var asyncResult = new SmtpWorkerAsyncResult(message, callback, state);
     SmtpClient newWorker = null;
     lock (queueLock)
     {
         // lock keeps queue count and active client count in sync
         messageQueue.Enqueue(asyncResult);
         newWorker = CheckNewWorker();
     }
     if (newWorker != null)
     {
         ProcessQueue(newWorker);
     }
     return asyncResult;
 }
        public IAsyncResult BeginSend(MailMessage message, AsyncCallback callback, object state)
        {
            var        asyncResult = new SmtpWorkerAsyncResult(message, callback, state);
            SmtpClient newWorker   = null;

            lock (queueLock)
            {
                // lock keeps queue count and active client count in sync
                messageQueue.Enqueue(asyncResult);
                newWorker = CheckNewWorker();
            }
            if (newWorker != null)
            {
                ProcessQueue(newWorker);
            }
            return(asyncResult);
        }
Esempio n. 7
0
        internal static void End(SmtpWorkerAsyncResult asyncResult)
        {
            // This method assumes that only 1 thread calls EndInvoke
            // for this object
            if (!asyncResult.IsCompleted)
            {
                // If the operation isn't done, wait for it
                asyncResult.AsyncWaitHandle.WaitOne();
                asyncResult.AsyncWaitHandle.Close();
                // Allow early GC
                asyncResult.m_AsyncWaitHandle = null;
            }

            // Operation is done: if an exception occured, throw it
            if (asyncResult.m_exception != null)
            {
                throw asyncResult.m_exception;
            }
        }
        private void ProcessQueue(SmtpClient clientToUse)
        {
            SmtpWorkerAsyncResult asyncResultToProcess = null;

            lock (queueLock)
            {
                // lock keeps queue count and active client count in sync
                if (messageQueue.Count > 0)
                {
                    asyncResultToProcess = messageQueue.Dequeue();
                    Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: ProcessQueue, active count = {1}, queue length = {2}", DateTimeOffset.Now, activeSmtpClients.Count, messageQueue.Count));
                }
                else
                {
                    // Nothing to process; shut down the SmtpClient
                    activeSmtpClients.Remove(clientToUse);
                }
            };
            if (asyncResultToProcess != null)
            {
                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: Before SendAsync, active count = {1}, queue length = {2}", DateTimeOffset.Now, activeSmtpClients.Count, messageQueue.Count));
                Exception exception = null;
                try
                {
                    clientToUse.SendAsync(asyncResultToProcess.Message, asyncResultToProcess);
                }
                catch (Exception ex)
                {
                    exception = ex;
                    Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "SendAsync Exception: {0}", exception));
                }
                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: After SendAsync, active count = {1}, queue length = {2}", DateTimeOffset.Now, activeSmtpClients.Count, messageQueue.Count));
                if (exception != null)
                {
                    asyncResultToProcess.Complete(exception, true);
                }
            }
            else
            {
                TryDisposeSmtpClient(clientToUse);
            }
        }
Esempio n. 9
0
            private void ThreadStart()
            {
                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: ThreadStart, pool count = {1}, queue length = {2}", DateTimeOffset.Now, pool.smtpWorkerPool.Count, pool.messageQueue.Count));
                SmtpClient smtpClient = null;

                try
                {
                    smtpClient = new SmtpClient();

                    Stopwatch idleTimeout = new Stopwatch();
                    bool      running     = true;

                    while (running)
                    {
                        SmtpWorkerAsyncResult asyncResultToProcess = null;
                        lock (pool.queueLock)
                        {
                            // lock keeps queue count and active client count in sync
                            if (pool.messageQueue.Count > 0)
                            {
                                asyncResultToProcess = pool.messageQueue.Dequeue();
                            }
                            else
                            {
                                if (idleTimeout.IsRunning && idleTimeout.ElapsedMilliseconds > idleTimeoutMilliseconds)
                                {
                                    pool.smtpWorkerPool.Remove(this);
                                    running = false;
                                }
                            }
                        }
                        if (asyncResultToProcess != null)
                        {
                            if (idleTimeout.IsRunning)
                            {
                                isIdle = false;
                                idleTimeout.Reset();
                            }
                            var       message   = asyncResultToProcess.Message;
                            Exception exception = null;
                            try
                            {
                                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: Before Send, pool count = {1}, queue length = {2}", DateTimeOffset.Now, pool.smtpWorkerPool.Count, pool.messageQueue.Count));
                                smtpClient.Send(message);
                                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: After Send, pool count = {1}, queue length = {2}", DateTimeOffset.Now, pool.smtpWorkerPool.Count, pool.messageQueue.Count));
                            }
                            catch (Exception ex)
                            {
                                exception = ex;
                                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "Error sending: {0}", exception));
                            }
                            asyncResultToProcess.Complete(exception, false);
                        }
                        else
                        {
                            if (!idleTimeout.IsRunning)
                            {
                                isIdle = true;
                                idleTimeout.Start();
                            }
                        }
                        Thread.Sleep(0);
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: Exception: {1}", DateTimeOffset.Now, ex));
                }
                finally
                {
                    if (smtpClient != null)
                    {
                        TryDisposeSmtpClient(smtpClient);
                    }
                }

                Debug.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:mm':'ss.ffffff}: ThreadEnding, pool count = {1}, queue length = {2}", DateTimeOffset.Now, pool.smtpWorkerPool.Count, pool.messageQueue.Count));
            }
        internal static void End(SmtpWorkerAsyncResult asyncResult)
        {
            // This method assumes that only 1 thread calls EndInvoke
            // for this object
            if (!asyncResult.IsCompleted)
            {
                // If the operation isn't done, wait for it
                asyncResult.AsyncWaitHandle.WaitOne();
                asyncResult.AsyncWaitHandle.Close();
                // Allow early GC
                asyncResult.m_AsyncWaitHandle = null;
            }

            // Operation is done: if an exception occured, throw it
            if (asyncResult.m_exception != null)
            {
                throw asyncResult.m_exception;
            }
        }