Solved

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

Posted on 2009-07-09
1
769 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to call weather service by using asmx? 4 44
How come this regular expression allows "e"? 14 45
MailAddress in vb 4 29
Problem to Office 1 16
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 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