none
multithreading + windows services + c#

    Question

  • hellow,

    I wants to send the "automated mails"to clients through windows services using multithreading in c#.I have completed the following code,but how to include multithreading in it?

    code is :

    using System;

    using System.Collections;

    using System.ComponentModel;

    using System.Data;

    using System.Diagnostics;

    using System.ServiceProcess;

    using System.Web.Mail;

    using System.Data.OleDb;

    using System.Timers;

    using System.Threading;

     

    namespace WindowsService_final

    {

    public class automatedmail : System.ServiceProcess.ServiceBase

    {

    private System.Data.OleDb.OleDbConnection conn;

    private System.ComponentModel.IContainer components;

    private System.Windows.Forms.Timer timer1;

    private System.Windows.Forms.NotifyIcon notifyIcon1;

    DateTime mdt=DateTime.Now;

    public automatedmail()

    {

    // This call is required by the Windows.Forms Component Designer.

    InitializeComponent();

    // TODO: Add any initialization after the InitComponent call

    }

    // The main entry point for the process

    static void Main()

    {

    System.ServiceProcess.ServiceBase[] ServicesToRun;

    // More than one user Service may run within the same process. To add

    // another service to this process, change the following line to

    // create a second service object. For example,

    //

    // ServicesToRun = new System.ServiceProcess.ServiceBase[] {new automatedmail(), new MySecondUserService()};

    //

    ServicesToRun = new System.ServiceProcess.ServiceBase[] { new automatedmail() };

    System.ServiceProcess.ServiceBase.Run(ServicesToRun);

    }

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    this.components = new System.ComponentModel.Container();

    this.conn = new System.Data.OleDb.OleDbConnection();

    this.timer1 = new System.Windows.Forms.Timer(this.components);

    this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);

    //

    // conn

    //

    this.conn.ConnectionString = @"Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDB:Database Locking Mode=1;Data Source=D:/mails.mdb;Jet OLEDB:Engine Type=5;Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Properties=;Mode=Share Deny None;Jet OLEDB:Encrypt Database=False;Jet OLEDB:Create System Database=False;Jet OLEDB:Don't Copy Locale on Compact=False;Jet OLEDB:Compact Without Replica Repair=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1";

    //

    // notifyIcon1

    //

    this.notifyIcon1.Text = "Automated Mail from jitendra";

    this.notifyIcon1.Visible = true;

    this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);

    //

    // automatedmail

    //

    this.CanHandlePowerEvent = true;

    this.CanPauseAndContinue = true;

    this.CanShutdown = true;

    this.ServiceName = "automatedmail";

    }

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    protected override void Dispose( bool disposing )

    {

    if( disposing )

    {

    if (components != null)

    {

    components.Dispose();

    }

    }

    base.Dispose( disposing );

    }

    DataSet ds=new DataSet();

    protected override void OnStart(string[] args)

    {

    timer1.Enabled=true;

    timer1.Start();

    DateTime dt=DateTime.Now;

    conn.Open();

    OleDbDataAdapter da=new OleDbDataAdapter("select * from tblautomatedmails",conn);

    da.Fill(ds);

    if(dt.ToShortDateString()==mdt.ToShortDateString())

    {

    foreach(DataRow dr in ds.Tables[0].Rows)

    {

    mdt=DateTime.Now.AddDays(+1);

    string mailtxt="";

    MailMessage mm=new MailMessage();

    mm.BodyFormat=MailFormat.Html;

    mm.To=dr["email"].ToString();

    mm.From="abc@abc.com";

    mm.Subject="Have a nice day";

    mailtxt = "<font face='verdana' color='#FF9900'><b>"+"Hi "+dr["email"].ToString()+"," + "</b></font><br><br>";

    mailtxt=mailtxt+"<font face='verdana' color='#FF0000'><b>"+"Good day" + "</b></font><br><br>";

    mailtxt=mailtxt+"<font face='verdana' color='#008080'><b>"+"May today be filled with sunshine and smile, laughter and love." + "</b></font><br><br>";

    mailtxt=mailtxt+"<font face='verdana' color='#0000FF'><b>Cheers!" + "<br><br>";

    mm.Body=mailtxt;

    SmtpMail.SmtpServer="localhost";

    SmtpMail.Send(mm);

    }

    }

    }

    /// <summary>

    /// Stop this service.

    /// </summary>

    protected override void OnStop()

    {

    // TODO: Add code here to perform any tear-down necessary to stop your service.

    conn.Close();

    }

    private void notifyIcon1_DoubleClick(object sender, System.EventArgs e)

    {

    }

    }

    }

     

    and finally it should be deployed on the web server.

    Please give me the above code which include multithreading n then how to deploy on web server?

    help..its really urgent..

    Monday, February 12, 2007 10:56 AM