轮询服务 - C# [英] Polling Service - C#
问题描述
将anobody能帮帮我吗?
我创建一个连接到SQL数据库和表检查的日期,并把它比作今天的一个窗口服务日期和更新该数据库如现场如果日期等于今天的日期,然后现场将被设置为true。
我遇到的问题是,当我开始它不这样做,该服务,但是当我在一个正常的形式做它完美
我的代码如下:
// System.Timers
定时器定时器=新的Timer();
保护覆盖无效的OnStart(字串[] args)
{
timer.Elapsed + =新ElapsedEventHandler(OnElapsedTime);
timer.Interval = 60000;
timer.Enabled = TRUE;
}
私人无效OnElapsedTime(对象源,ElapsedEventArgs E)
{
INT campid = 0;
变种campRes =新ROS.Process.CampaignServiceController()GetCampainInfo();
的foreach(在campRes变种S)
{
campid = s.CampaignId;
如果(s.CampEndDate.Date< DateTime.Today.Date)
{
// WriteDataToFile(不活动+ campid.ToString());
新ROS.Process.CampaignServiceController()SetCampainStatusFalse(campid)。
}
,否则
{
// WriteDataToFile(活动:+ campid.ToString());
新ROS.Process.CampaignServiceController()SetCampainStatusTrue(campid)。
}
}
}
这样做是等待一个事件而不是使用定时器的另一种方法。
即。
公共类PollingService
{
私人螺纹_workerThread;
私人的AutoResetEvent _finished;
私人const int的_TIMEOUT = 60 * 1000;
公共无效StartPolling()
{
_workerThread =新主题(投票);
_finished =新的AutoResetEvent(假);
_workerThread.Start();
}
私人无效投票()
{
,而(!_finished.WaitOne(_TIMEOUT))
{
//做任务
}
}
公共无效StopPolling()
{
_finished.Set();
_workerThread.Join();
}
}
在您的服务
公共部分类服务1:ServiceBase
{
私人只读PollingService _pollingService =新PollingService();
公共服务1()
{
的InitializeComponent();
}
保护覆盖无效的OnStart(字串[] args)
{
_pollingService.StartPolling();
}
保护覆盖无效调用OnStop()
{
_pollingService.StopPolling();
}
}
Will anobody be able to help me?
I am creating a windows service that connects to a sql database and checks a date in the table and compares it to todays date and updates a field in that database for eg if the date is equal to todays date then the field will be set to true.
The problem I am having is that when i start the service it does not do that but when i do it in a normal form it works perfectly.
My code is below:
//System.Timers
Timer timer = new Timer();
protected override void OnStart(string[] args)
{
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 60000;
timer.Enabled = true;
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
int campid = 0;
var campRes = new ROS.Process.CampaignServiceController().GetCampainInfo();
foreach (var s in campRes)
{
campid = s.CampaignId;
if (s.CampEndDate.Date < DateTime.Today.Date)
{
//WriteDataToFile("Not Active : " + campid.ToString());
new ROS.Process.CampaignServiceController().SetCampainStatusFalse(campid);
}
else
{
//WriteDataToFile("Active : " + campid.ToString());
new ROS.Process.CampaignServiceController().SetCampainStatusTrue(campid);
}
}
}
Another way of doing this would be to wait on an event rather then using a timer.
i.e.
public class PollingService
{
private Thread _workerThread;
private AutoResetEvent _finished;
private const int _timeout = 60*1000;
public void StartPolling()
{
_workerThread = new Thread(Poll);
_finished = new AutoResetEvent(false);
_workerThread.Start();
}
private void Poll()
{
while (!_finished.WaitOne(_timeout))
{
//do the task
}
}
public void StopPolling()
{
_finished.Set();
_workerThread.Join();
}
}
In your Service
public partial class Service1 : ServiceBase
{
private readonly PollingService _pollingService = new PollingService();
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
_pollingService.StartPolling();
}
protected override void OnStop()
{
_pollingService.StopPolling();
}
}
这篇关于轮询服务 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!