开发者

Schedule a C# application

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 plea开发者_运维百科se 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


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.


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.


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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜