Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

window service in c#

Posted on 2014-10-10
8
Medium Priority
?
199 Views
Last Modified: 2014-10-24
I have below codes working fine in window service. Can you show me how to use timer?
My goal is to send email daily at 1:00 am (PST).

Thanks


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Net.Mail;
using System.Threading;

namespace ConsoleApplication1
{
    class Program : ServiceBase
    {
        static void Main(string[] args)
        {
            ServiceBase.Run(new Program());
        }
        public Program()
        {
            this.ServiceName = "My Service";
        }

       protected override void OnStart(string[] args)
          {
            base.OnStart(args);
            //TODO: place your start code here
             
            #region main code
            sendSingleEmail("email", "email", "test", "test", true);
            #endregion



          }
        protected override void OnStop()
          {
            base.OnStop();
            //TODO: clean up any variables and stop any threads
          }

        #region method:send email
        public static void sendSingleEmail(string from, string to, string subject, string body, bool isHtml)
        {
            String smtpServer = "smtp.com";
            System.Net.Mail.MailMessage MyMessage = new System.Net.Mail.MailMessage();
            MyMessage.Subject = subject;
            MyMessage.Body = body;
            MyMessage.From = new MailAddress(from);
            MyMessage.To.Add(to);
            MyMessage.IsBodyHtml = isHtml;
            SmtpClient emailClient = new SmtpClient(smtpServer,25);
            emailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            emailClient.EnableSsl = false;
            emailClient.Send(MyMessage);            
        }
        #endregion
    }
     
}
0
Comment
Question by:ITsolutionWizard
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 41

Expert Comment

by:Kyle Abrahams
ID: 40373987
You shouldn't use a service for this.  A service is something that runs constantly, looping at an interval to perform tasks.  There is no reason to keep your program in memory for that long if all your doing is sending an email once every day.

What I would recommend is a windows console application that when launched will send your email.  This is then scheduled via a scheduled task on a server/workstation.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40374046
I have applications like this that need to run at interval. I always build plain-old Windows Forms and I schedule them in the Windows Tasks Scheduler. This way, it is easy for me to run anytime, to debug, ...

Check http://emoreau.com/Entries/Articles/2008/11/Passing-arguments-to-an-application-startup.aspx
0
 

Expert Comment

by:CHARU T
ID: 40377723
Its best written as a winforms program with Timer class and its tick() used to send the email with smtpclient. Registering as a service is needed when it actually is a product. It can be run as a background task as well thats starts at windows restart.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40378216
what I don't like about a Windows app with a Timer is if an error occurs, it is sometime not able to restart properly. If it is a scheduler that runs it, it always starts fresh. Also, a schedule task can run even if the computer is not logged on.
0
 

Expert Comment

by:CHARU T
ID: 40378342
You can handle all the errors and what to do, if conditions. Also, whether the system is logged on or restarted, such things can also be tested by subscribing to windows events in C# code itself. You can log errors onto a text file, and proceed. Make sure there is no pop-up message boxes so its run in the background without user intervention.
0
 
LVL 1

Author Comment

by:ITsolutionWizard
ID: 40378521
Need codes sample
0
 

Expert Comment

by:CHARU T
ID: 40378674
If you need code sample for Timer (?), then there are plenty in the web. Add a timer and do your email fn in the tick() event. You can also write a console app, and use a timer in it, log msgs and errors to a text file so there are no pop-ups.

What is your q exactly here?
0
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 40380107
I would agree with Eric on this . . . if there's an error the code might exit and not be called again.  With a scheduled task you're relying on the windows scheduler to call the executable.

For a console application:
sendSingleEmail  becomes the main of the program.  Then schedule the task on the computer and you're done.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

916 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