?
Solved

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

Posted on 2009-07-09
1
Medium Priority
?
807 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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