Solved

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

Posted on 2009-07-09
1
743 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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Change to event 1 72
.xltm file opens as .xlsx file 3 36
The Using statement in C# 5 35
Trouble with References... 5 21
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

771 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

12 Experts available now in Live!

Get 1:1 Help Now