Developing a windows service

I'm using VS2013 Community and I notice there isn't a project template for a Windows Service which would be ideal for my project.

What I need to do is collect some data from a database to which client applications store data on a daily basis, perform calculations on that data and upload the results. This needs to be done once daily at a certain time of day.

If I can't develop a windows service to do this using VS 2013 (or even 2012), I do have 2010 on another computer where I can do it…

Regardless of where the service is developed, I have the following code as sort of concept… I wrote this in a rush today based on a brief skim of a tutorial. I'd appreciate it if you could look it over and let me know if it'll work okay:

private class MyService { Timer t1 = new Timer(); private void OnStart(object sender, EventArgs e) { t1.Elapsed += new ElapsedEventHandler(do_work); t1.Interval = 86400000; // 1 day / 24 hours t1.Enabled = true; if (DateTime.Now.TimeOfDay == 4) { t1.Start(); } } private void do_work(object sender, EventArgs e) { // do the work of the service... } } 

So my questions are:

  • Is a Windows Service the best way to do this? I don't want to have to start an application every day to get this done.
  • Is the code I've pasted for a Windows Service okay? Will it do the job?

by ortund via /r/csharp

Leave a Reply