4

Will anobody be able to help me?

I am creating a windows service that connects to a sql database and checks a date in the table and compares it to todays date and updates a field in that database for eg if the date is equal to todays date then the field will be set to true.

The problem I am having is that when i start the service it does not do that but when i do it in a normal form it works perfectly.

My code is below:

//System.Timers
Timer timer = new Timer();
protected override void OnStart(string[] args)
{
    timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
    timer.Interval = 60000;
    timer.Enabled = true;
}

private void OnElapsedTime(object source, ElapsedEventArgs e)
{
    int campid = 0;
    var campRes = new ROS.Process.CampaignServiceController().GetCampainInfo();

    foreach (var s in campRes)
    {
        campid = s.CampaignId;

        if (s.CampEndDate.Date < DateTime.Today.Date)
        {
            //WriteDataToFile("Not Active : " + campid.ToString());
            new ROS.Process.CampaignServiceController().SetCampainStatusFalse(campid);
        }
        else
        {
            //WriteDataToFile("Active : " + campid.ToString());
            new ROS.Process.CampaignServiceController().SetCampainStatusTrue(campid);
        }
    }
}
mellamokb
  • 56,094
  • 12
  • 110
  • 136
greame
  • 41
  • 1
  • 1
  • 2

2 Answers2

11

Another way of doing this would be to wait on an event rather then using a timer.

i.e.

    public class PollingService
    {
        private Thread _workerThread;
        private AutoResetEvent _finished;
        private const int _timeout = 60*1000;

        public void StartPolling()
        {
            _workerThread = new Thread(Poll);
            _finished = new AutoResetEvent(false);
            _workerThread.Start();
        }

        private void Poll()
        {
            while (!_finished.WaitOne(_timeout))
            {
                //do the task
            }
        }

        public void StopPolling()
        {
            _finished.Set();
            _workerThread.Join();
        }
    }

In your Service

    public partial class Service1 : ServiceBase
    {
        private readonly PollingService _pollingService = new PollingService();
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            _pollingService.StartPolling();
        }

        protected override void OnStop()
        {
            _pollingService.StopPolling();
        }

    }
Darryl Braaten
  • 5,229
  • 4
  • 36
  • 50
0

Set Timer.AutoReset = true. otherwise it will do its work only one time. but it's better to work with threading in windows services.

[edit] ah, yes. autoreset is true in default. I put this too in my code: GC.KeepAlive( myTimer ); so the gc won't remove it if it is inactive.

ibram
  • 4,414
  • 2
  • 22
  • 34
  • but isnt it true that if you ommit the Timer.AutoReset the compiler assumes that it is true? – greame May 12 '11 at 16:25
  • okay so you say that by default the garbage collector discards the timer when it is not in the ontimeelapsed event? – greame May 12 '11 at 16:38
  • I'm not sure where I had read this. may be here: http://stackoverflow.com/questions/477351/in-c-where-should-i-keep-my-timers-reference/477355#477355 – ibram May 12 '11 at 16:41
  • here I found something you can use: http://www.codeguru.com/columns/dotnet/article.php/c6919 – ibram May 12 '11 at 16:45