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

  • Home
  • SEARCH
  • 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 6009001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:54:30+00:00 2026-05-23T01:54:30+00:00

I am building a Windows Service that will audit users based on a schedule

  • 0

I am building a Windows Service that will audit users based on a schedule in a database. The service checks the database for scheduled audits every ten minutes. Once and audit is started, it marks a start time in the database table so it will not start again if it takes longer than ten minutes.

My question is, is the following code acceptable for what I am doing and should I use multithreading every time 10 minutes is elapsed and if so, how would I accomplish this?

My sample code:

protected override void OnStart(string[] args)
{
    var aTimer = new Timer(600000);
    aTimer.Elapsed += ATimerElapsed;
    aTimer.Interval = 600000;
    aTimer.Enabled = true;
    GC.KeepAlive(aTimer);
}

private static void ATimerElapsed(object sender, ElapsedEventArgs e)
{
    try
    {
         Worker.ProcessScheduledAudits();
    }
    catch (Exception ex)
    {
         EventLog.WriteEntry("Application", ex.Message, EventLogEntryType.Error);
    }                
}
  • 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-05-23T01:54:30+00:00Added an answer on May 23, 2026 at 1:54 am

    A System.Threading.Timer will use a thread from the Threadpool to run the Elapsed handler(s). So, you’re already using multithreading just by using the timer. In fact, ALL timers use a background thread of some kind; they just differ on how they multithread in order to do what makes the most sense for the intended usage.

    If you need to run something once every ten minutes, but also make sure that two handlers are never running at the same time, try setting a “CurrentlyRunning” flag in the Elapsed method, and examining it before doing any heavy lifting.

        protected override void OnStart(string[] args)
        {
            var aTimer = new Timer(600000);
            aTimer.Elapsed += ATimerElapsed;
            aTimer.Interval = 600000;
            aTimer.Enabled = true;
            GC.KeepAlive(aTimer);
        }
    
        private static currentlyRunning;
    
        private static void ATimerElapsed(object sender, ElapsedEventArgs e)
        {
            if(currentlyRunning) return;
            currentlyRunning = true;
            try
            {
                Worker.ProcessScheduledAudits();
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Application", ex.Message, EventLogEntryType.Error);
            }
            currentlyRunning = false;
        }
    

    Theoretically, this could race, but as you’re only kicking off a thread for this event every 10 minutes the odds are EXTREMELY unlikely.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a Windows service that performs different actions based on which Windows user
I have created a Windows Service in VB.net for building xML from a database.
I am building a web service that will make heavy use of cross-domain GET
I'm in the process of building a Windows Service that implements MEF. The idea
Im building my first Windows Service. It's a component that connects to a mailbox
I'm building a Windows Service application that takes as input a directory containing scanned
I'm building a product that involves a windows service caching data on the local
I am building an application that will have web, windows and mobile (iPhone) client,
I'm building a Windows Service base class to manages the polling off a schedule's
I'm building a multi-threaded windows service application in Delphi XE2 which uses ADO database

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.