views:

62

answers:

2

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);
        }
    }
}
+2  A: 

Use a System.Threading.Timer to fire the process off at the scheduled interval.

Justin Niessner
Agree. `Thread.Sleep()` is a sign of a bad design.
Al Bundy
@al: why Thread.Sleep is bad?
matti
+2  A: 

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.

Aaronaught
The above code works like a charm. I'm using in a windows service to check a queue.
Chuck Conway
thanks! ...hmmm... so you suggest I don't even call ObjectFolderApp.Initialize();in OnStart?? It has to be initialized only once. Another question is calling overrided base methods. Why is that?
matti
@matti: I don't know what you're doing in `Initialize`, so it's hard to say. You might still need it. As for calling the base methods, you don't necessarily have to, but as a general rule it's a good idea if what you're trying to do is *extend* the base class logic as opposed to *replace* it (which is the desired semantic here).
Aaronaught
Initialize is for reading configuration file etc. the normal init tasks.
matti