Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7909865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T12:37:52+00:00 2026-06-03T12:37:52+00:00

I have built a windows service to populate a database with my email inbox

  • 0

I have built a windows service to populate a database with my email inbox every 5 minutes.

I used a class inside my windows service the class gets my emails and writes them to my database, the class has been tested and works.

All i need the windows service to do is use a timer and call the class every 5 minutes, but i have no idea whats going on as i cant even test my windows service.

Please someone tel me what to do to test, if there is a way to test, or just blink luck and pray it works lol.

Also do u have to uninstall and re-install every time you want to test the service or is there an update service option? Please answer this i’m really interested even tho its not my main question.

This is my windows service, if u can point out any errors that would be amazing since i cant test for them. I think my timer might be wrong if some one could look at it?

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.Timers;

namespace EmailWindowsService
{
    public partial class MyEmailService : ServiceBase
    {
        private Timer scheduleTimer1 = null;
        private DateTime lastRun;
        private bool flag;

        public MyEmailService()
        {
            InitializeComponent();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource", "MyNewLog");
            }
            eventLogEmail.Source = "MySource";
            eventLogEmail.Log = "MyNewLog";

            scheduleTimer1 = new Timer();
            scheduleTimer1.Interval = 5 * 60 * 1000;
            scheduleTimer1.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);
        }

        protected override void OnStart(string[] args)
        {
            flag = true;
            lastRun = DateTime.Now;
            scheduleTimer.Start();
            eventLogEmail.WriteEntry("Started");
        }

        protected override void OnStop()
        {
            scheduleTimer.Stop();
            eventLogEmail.WriteEntry("Stopped");
        }
        protected override void OnPause()
        {
            scheduleTimer.Stop();
            eventLogEmail.WriteEntry("Paused");
        }
        protected override void OnContinue()
        {
            scheduleTimer.Start(); ;
            eventLogEmail.WriteEntry("Continuing");
        }
        protected override void OnShutdown()
        {
            scheduleTimer.Stop();
            eventLogEmail.WriteEntry("ShutDowned");
        }

        protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            RetriveEmailClass Emails = new RetriveEmailClass();
            if (flag == true)
            {

                eventLogEmail.WriteEntry("In getting Email Method");
                Emails.ServiceEmailMethod();
                lastRun = DateTime.Now;
                flag = false;
            }
            else if (flag == false)
            {
                if (lastRun.Date < DateTime.Now.Date)
                {
                    Emails.ServiceEmailMethod();
                    eventLogEmail.WriteEntry("In getting Email Method");
                }
            }
        }
        }


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T12:37:54+00:00Added an answer on June 3, 2026 at 12:37 pm

    Surely you can test it. All you need is

    1. start up the service
    2. observe that it triggers the expected call after 5 minutes
    3. (observe that it triggers the expected call every 5 minutes for a couple more times)

    You can test this manually, or (preferably) create/use an automated test harness which allows you to test repeatedly and reliably, as many times as you want. This is possible even using a simple batch file.

    To detect that the timer works correctly, you can inspect its log file. It also helps of course if you make the called class method configurable instead of hardcoding it. So you can run your automated tests using a dummy worker class which does not flood your inbox 🙂

    To make it even more testable, you can extract the timing logic from your service class too, so that it becomes runnable from a regular application. Then you can test it even easier, even using a unit test framework such as NUnit. This allows you to do more thorough testing, using different timing intervals etc. And the service class itself becomes an almost empty shell whose only job is to launch and call the other classes. If you have verified that all the classes containing real program logic (i.e. all the code which can fail) is unit tested and works fine, you can have much greater confidence in that your whole app, when integrated from its smaller parts, works correctly too.

    Update

    Looking through your code, it seems that you don’t initialize flag anywhere, so its default value will be false. You should initialize it to true in the constructor, otherwise your email retriever won’t ever get called even if the timer fires properly.

    To set the interval to 1 minute, my first guess would be

    scheduleTimer1.Interval = 1 * 60 * 1000;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built one windows service that sends out email after every 30 minutes
I have a Windows service built upon ATL 7's CAtlServiceModuleT class. This service serves
I have built a windows service application in VB.net 2008, and used the Setup
I have built setup file for my windows service in visual studio 2010.Its working
We have built a Windows Service that is running on client's machines, which occasionally
We have built a .NET Windows Service that we install on client PCs to
I want to create an installer for windows service built in C++. I have
I have written a .NET Windows service which has a WCF service built into
I have built a Windows Service using VS 2008 targeting Framework 3.5. When I
I have an application that is built as a Windows Service and a c#

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.