overlooping在C# [英] overlooping in C#

查看:129
本文介绍了overlooping在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立在C#中的系统服务。

I am going to create a system service in C#.

在的OnStart部分,我想每30秒循环和查询MySQL数据库。如果其行是大于0,我会处理使用faxcom图书馆借了一些传真

In the onstart section I would like to loop every 30 seconds and query a mysql database. If numrows are greater than 0 I will process some faxes using the faxcom library.

我的问题是:循环每30秒用尽程序/电脑?什么是最好的函数/方法用于循环和睡眠?你有循环的示例代码和睡觉?

My question is: Would looping every 30 seconds exhaust the program/computer? What would be the best function/method to use for the loop and sleep? Do you have any example code for the loop and sleep?

推荐答案

使用 Thread.sleep代码()将是一个坏的解决方案,因为即使睡觉时你的线程处于活动状态。使用定时类,而不是和处理其消逝事件。

Using Thread.Sleep() would be a bad solution, because even while sleeping your thread is active. Use Timer class instead and handle its Elapsed event.

这篇文章研究不同的方法来解决你的服务的定期执行。

This article examines different ways to tackle the periodical execution of your service.

下面是你的OnStart方法可能看起来是这样的:使用

Here is what your OnStart method might look like:

using System.Timers; 

private timer = new Timer();

protected override void OnStart(string[] args)
{
 timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
 timer.Interval = 30000; // every 30 seconds
 timer.Enabled = true;
}

Private void OnElapsedTime(object source, ElapsedEventArgs e)
{
   // Execute your code here
}

这篇关于overlooping在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆