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 710395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:36:57+00:00 2026-05-14T04:36:57+00:00

My question is that is this the best practice to do this. Couldn’t find

  • 0

My question is that is this the best practice to do this. Couldn’t find any good examples. I have following code in file created by VS2005:

public partial class ObjectFolder : ServiceBase
{
    protected override void OnStart(string[] args)
    {
        ObjectFolderApp.Initialize();

        ObjectFolderApp.StartMonitorAndWork();
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop yourservice.
    } 
}

then:

class ObjectFolderApp
{
    public static bool Initialize()
    {
        //all init stuff
        return true;
    }


    public static void StartMonitorAndWork()
    {
        Thread worker = new Thread(MonitorAndWork);
        worker.Start();
    }


    private static void MonitorAndWork()
    {
        int loopTime = 60000;
        if (int.TryParse(_cfgValues.GetConfigValue("OfWaitLoop"), out loopTime))
            loopTime = 1000 * loopTime;

        while (true)
        {
            /* create+open connection and fill DataSet */
            DataSet ofDataSet = new DataSet("ObjectFolderSet");
            using (_cnctn = _dbFactory.CreateConnection())
            {
                _cnctn.Open();

                //do all kinds of database stuff
            }
            Thread.Sleep(loopTime);
        }
    }
}
  • 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-14T04:36:57+00:00Added an answer on May 14, 2026 at 4:36 am

    Re-hashing my answer from this question, the recommended way is to use a timer and the following code:

    public class MyService : ServiceBase
    {
        private Timer workTimer;    // System.Threading.Timer
    
        protected override void OnStart(string[] args)
        {
            workTimer = new Timer(new TimerCallback(DoWork), null, 5000, 5000);
            base.OnStart(args);
        }
    
        protected override void OnStop()
        {
            workTimer.Dispose();
            base.OnStop();
        }
    
        private void DoWork(object state)
        {
            RunScheduledTasks();  // Do some work
        }
    }
    

    Simple!

    Note that the Timer type being used is System.Threading.Timer, same as Justin specifies.

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

Sidebar

Related Questions

Question: I have a question that is apparently not answered by this already-asked Bash
I have a best practice type of question regarding Nibs and UISegmentedControls. I have
I realize that this question is impossible to answer absolutely, but I'm only after
Alright, I am going to state up front that this question may be too
I know that this is a simple question for PHP guys but I don't
This is a question that's been nagging me for some time. I always thought
this is a question that when programming I always wonder: What to use when
This is a question that was sparked by Rob Walker 's answer here .
This is also a question that I asked in a comment in one of
I realise that this is a very basic question, but it is one which

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.