views:

74

answers:

4

Hello, i developed an application to connect to a pop3 mail, read the emails and insert them into a database. I need this service to do the task once an hour. Could somebody please help me with this because i'm stuck?

static void Main(string[] args)
  {
   TcpClient Server;
   NetworkStream NetStrm=null;
   StreamReader RdStrm=null;
   string Data, tmpMessage="", mail="", szTemp, CRLF = "\r\n", ch="";
   byte[] szData;
   int i=0, counter=0;
   MySqlConnection connection = new MySqlConnection();
   [snip]
   Console.WriteLine(RdStrm.ReadLine());
   connection.Close();
   Console.ReadLine();
   }

I'm not much of a programmer so please excuse my bad use of the language. All the functionality is inside here but i just don't know how to time it and make it run continously. Thanks a lot

A: 

There are alot of tutorials out there that explain how to write a service - I suggest you start there (just type "C# service tutorial" in google)

deltreme
+4  A: 

Hi John,

If you want to use your application "as is", then you can configure it using Windows Task Scheduler, a service that is included in every version of Windows.

Another approach is to create a dedicated Windows service, but this requires you to rewrite your application from being a console app to a Windows service (used to be called "NT Service").

Hope this helps.

Vagif Abilov
Don't forget to remove the `Console.ReadLine()`. :)
bzlm
lol! Yeah, that would be a bummer. :-)
Robert Giesecke
-1 for service. Seriously - a simple downloader running ONCE PER HOUR is pretty much the worst candidate for a service ever. Scheduled task.
TomTom
A: 

And if you don't want to implement the nitty gritty details yourselv, there are nice libraries out there, like <shamelessplug>DemiCode Scheduler</shamelessplug> and Quartz.NET.

Peter Lillevold
Scheduling is built into Windows. :) http://msdn.microsoft.com/en-us/library/aa383614(VS.85).aspx
bzlm
@bzlm - sure, a general purpose scheduler service. But its not a .Net API for integration into custom applications and process hosts.
Peter Lillevold
A: 

Forget about windows services - this is aruthless approach. The main problem here: A service is alive all the time. You want a simple process to start once per hour, taking maybe 2 minutes.... and not to have that thing clog up your memory for the rest of the hour.

Go with the task scheduler in Windows. Make a command line application to do the download, start it once per hour or whatever schedule you feel like (hourly during the night, every 15 minutes during the day) with the scheduler. Finished.

http://en.wikipedia.org/wiki/Task_Scheduler has a good link on how that works.

TomTom