Solved

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

Posted on 2009-07-09
1
782 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 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