Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

window service in c#

Posted on 2014-10-10
8
Medium Priority
?
198 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
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

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
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 
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 40

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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