Solved

Using the treadpool?ThreadPool.QueueUserWorkItem(eu.SendEmailAsync("daddy longlegs", "mcupryk@koretelematics.com"));

Posted on 2009-07-09
1
752 Views
Last Modified: 2012-05-07
  // Respond to the Elapsed event of the timer control
        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            LogMessage.LogMessages("Service Running");
            EmailUtil eu = new EmailUtil();
            eu.HtmlFilePath = @"c:\Temp\readme.htm";
            eu.Subject = "test message";
            eu.SmtpServer = "172.17.1.4";
            eu.FromEmail = "mcupryk@koretelematics.com";
            ///loop here through your datatable etc. of email recipients ---)
            ThreadPool.QueueUserWorkItem(eu.SendEmailAsync("daddy longlegs", "mcupryk@koretelematics.com"));

Error	1	The best overloaded method match for 'System.Threading.ThreadPool.QueueUserWorkItem(System.Threading.WaitCallback)' has some invalid arguments	C:\Inetpub\wwwroot\EmailSubSystemService\EmailSubSystemService\EmailService.cs	100	13	EmailSubSystemService

Error	2	Argument '1': cannot convert from 'string' to 'System.Threading.WaitCallback'	C:\Inetpub\wwwroot\EmailSubSystemService\EmailSubSystemService\EmailService.cs	100	42	EmailSubSystemService
 

--------------------------------------------------------------------------------------------------------

using System;

using System.Data;

using System.Threading;

using System.Runtime.Remoting;

using System.Runtime.Remoting.Messaging;

using System.Text;

using System.Web.Security;

using System.Diagnostics;

using System.IO;

using System.Net.Mail;
 
 

namespace EmailSubSystemService

{

    public class EmailUtil

    {

        private string htmlFilePath;

        public string HtmlFilePath

        {

            get

            {

                return htmlFilePath;

            }

            set

            {

                htmlFilePath = value;

            }

        }

        private string subject;

        public string Subject

        {

            get

            {

                return subject;

            }

            set

            {

                subject = value;

            }

        }
 

        private string smtpServer;

        public string SmtpServer

        {

            get

            {

                return smtpServer;

            }

            set

            {

                smtpServer = value;

            }

        }
 

        private string fromEmail;

        public string FromEmail

        {

            get

            {

                return fromEmail;

            }
 

            set

            {

                fromEmail = value;

            }

        }
 

        public string strBody = String.Empty;
 

        public string SendEmail(string name, string emailAddress)

        {

            if (strBody == String.Empty)

            {

                try

                {

                    using (StreamReader reader = new StreamReader(this.HtmlFilePath))

                    {

                        strBody = reader.ReadToEnd();

                    }

                }

                catch (Exception ex)

                {

                    throw new Exception("error reading HTML File" + ex.Message);

                }

            }

            try

            {

                MailMessage Message = new MailMessage();

                Message.IsBodyHtml =true;

                Message.To.Add(emailAddress);

                Message.From = new System.Net.Mail.MailAddress(this.FromEmail);

                Message.Subject = this.Subject;

                Message.Body = strBody;

                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(this.SmtpServer);

                smtp.Send(Message);

            }

            catch(SmtpException ehttp)

            {

                throw new Exception("Send error" + ehttp.Message);

            }

            return "sent" + name;

        }

        public delegate string SendEmailDelegate(string name, string emailAddress);
 

        public void GetResultsOnCallback(IAsyncResult ar)

        {

            SendEmailDelegate del = (SendEmailDelegate)

             ((AsyncResult)ar).AsyncDelegate;

            try

            {

                string result;

                result = del.EndInvoke(ar);

                Debug.WriteLine("\nOn CallBack: result is " + result);

            }

            catch (Exception ex)

            {

                Debug.WriteLine("\nOn CallBack, problem occurred: " + ex.Message);

            }

        }
 

        public string SendEmailAsync(string name, string emailAddress)

        {

            SendEmailDelegate dc = new SendEmailDelegate(this.SendEmail);

            AsyncCallback cb = new AsyncCallback(this.GetResultsOnCallback);

            IAsyncResult ar = dc.BeginInvoke(name, emailAddress, cb, null);

            return "ok";

        }

    } // end class EmailUtil 

}

Open in new window

0
Comment
Question by:mathieu_cupryk
1 Comment
 
LVL 5

Accepted Solution

by:
Pablissimo earned 500 total points
ID: 24811956
You're using a method call as if it's a delegate - and you needn't worry about the threadpool at all.

It should be sufficient to make the call
eu.SendEmailAsync("daddy longlegs", "mcupryk@koretelematics.com")
directly in the timer event without worrying about the threadpool, because your implementation of that method.

The BeginInvoke method of delegates invokes the delegate using a ThreadPool thread automatically - you don't need to supply that logic.

See http://msdn.microsoft.com/en-us/library/2e08f6yc(VS.71).aspx
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now