安排 C# 应用程序
我开发了一个应用程序来连接到 pop3 邮件、读取电子邮件并将其插入数据库。我需要这项服务每小时执行一次任务。有人可以帮我解决这个问题吗,因为我被困住了?
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想“按原样”使用您的应用程序,则可以使用 Windows 任务计划程序(每个版本的 Windows 中都包含的一项服务)对其进行配置。
另一种方法是创建专用的 Windows 服务,但这需要您将应用程序从控制台应用程序重写为 Windows 服务(过去称为“NT 服务”)。
希望这有帮助。
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.
忘记 Windows 服务——这是无情的做法。这里的主要问题是:服务始终处于活动状态。您想要一个简单的过程每小时启动一次,大约需要 2 分钟……并且不要让这个事情在接下来的一小时内堵塞您的记忆。
使用 Windows 中的任务计划程序。创建一个命令行应用程序来执行下载,每小时启动一次,或者使用调度程序以您喜欢的任何时间表(晚上每小时一次,白天每 15 分钟一次)启动。完成的。
http://en.wikipedia.org/wiki/Task_Scheduler 有一个很好的链接,介绍了如何做到这一点作品。
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.
如果您不想自己实现具体细节,可以使用一些不错的库,例如DemiCode Scheduler 和 Quartz.NET。
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.
有很多教程解释如何编写服务 - 我建议您从那里开始(只需在 google 中输入“C# 服务教程”)
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)