how to implement windows service loop that waits for a period in C# / .NET2.0
- by matti
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);
        }
    }
}