locked
Windows Service Question RRS feed

  • Question

  • Hi,

    I have crrated below windows service and installed it using InstallShield. I am able to see the windows service Started in the Control Panel-Services but I am not getting any email when the minutes turns to 30.

    Can anyone help please...

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Net.Mail;
    
    namespace MyTestWindowsService
    {
        public partial class MyTestWindowsService : ServiceBase
        {
            public MyTestWindowsService()
            {
                InitializeComponent();
    
                if (!System.Diagnostics.EventLog.SourceExists("MyTestWindowsService"))
                {
                    System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
                }
    
                eventLog1.Source = "MyEventLogSource";
                eventLog1.Log = "MyNewLog Comes here!";
            }
    
            protected override void OnStart(string[] args)
            {
                eventLog1.WriteEntry("In OnStart");
            }
    
            protected override void OnStop()
            {
                eventLog1.WriteEntry("In onStop.");
            }
    
            protected override void OnContinue()
            {
                eventLog1.WriteEntry("In OnContinue.");
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                if (DateTime.Now.Minute == 30)
                {
                    eventLog1.WriteEntry("starting to send email.");
    
                    try
                    {
                        MailMessage mail = new MailMessage();
                        SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
    
                        mail.From = new MailAddress("it@mydomain.com");
                        mail.To.Add("jassim@mydomain.com");
                        mail.Subject = "Test Mail";
                        mail.Body = "This is for testing SMTP mail from GMAIL";
    
                        SmtpServer.Port = 587;
                        SmtpServer.Credentials = new System.Net.NetworkCredential("it@mydomain.com", "mypassword");
                        SmtpServer.EnableSsl = true;
    
                        SmtpServer.Send(mail);
    
                        eventLog1.WriteEntry("email sent.");
                    }
                    catch (Exception ex)
                    {
                        // MessageBox.Show(ex.ToString());
                        eventLog1.WriteEntry("failed sending email.");
                    }
                }
            }
        }
    }
    



    Sunday, August 24, 2014 8:02 AM

Answers

  • Don't you need to register the  timer event with "+=" like in the code below?

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Timer timer1 = new Timer();
            public Form1()
            {
                InitializeComponent();
                timer1.Tick += new EventHandler(timer1_Tick);
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
            }
        }
     
    
    }


    jdweng

    • Proposed as answer by Fred Bao Wednesday, September 3, 2014 6:48 AM
    • Marked as answer by Fred Bao Monday, September 8, 2014 8:18 AM
    Sunday, August 24, 2014 12:39 PM

All replies

  • It's hard for anyone to be able to help you when you don't share the complete code but only a partial class. What kind of Timer class are you using and did you make sure that this Timer is actually started somewhere? Does the "starting to send email" message ever gets written to the log? Otherwise, the Tick event handler is never being executed at all. Also, you should use a System.Timers.Timer or System.Threading.Timer in a windows service:

    http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx
    http://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx

    Neither of these timers have any Tick event. A System.Windows.Forms.Timer should only be used in a Windows Forms application.

    I suggest you take a look at the example of how to use a System.Timers.Timer on the following page: http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

    Sunday, August 24, 2014 9:20 AM
  • It's hard for anyone to be able to help you when you don't share the complete code but only a partial class

    1. hey! it;s all the code :) I am just starting with windows service with this sample only :)
    2. I am using the System.Timers.Timer
    3. The service is not starting (it's just Automatic) therefore nothing is getting wriiten in the log
    Sunday, August 24, 2014 9:38 AM
  • Don't you need to register the  timer event with "+=" like in the code below?

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            Timer timer1 = new Timer();
            public Form1()
            {
                InitializeComponent();
                timer1.Tick += new EventHandler(timer1_Tick);
            }
            private void timer1_Tick(object sender, EventArgs e)
            {
            }
        }
     
    
    }


    jdweng

    • Proposed as answer by Fred Bao Wednesday, September 3, 2014 6:48 AM
    • Marked as answer by Fred Bao Monday, September 8, 2014 8:18 AM
    Sunday, August 24, 2014 12:39 PM