轮询服务 - C#

发布于 2024-11-07 00:34:57 字数 1079 浏览 0 评论 0原文

有人能帮助我吗?

我正在创建一个连接到 sql 数据库并检查表中的日期并将其与今天的日期进行比较并更新该数据库中的字段的 Windows 服务,例如,如果日期等于今天的日期,则该字段将设置为 true 。

我遇到的问题是,当我启动服务时,它不会这样做,但是当我以正常形式执行它时,它工作得很好。

我的代码如下:

//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);
        }
    }
}

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆一缕阳光ご 2024-11-14 00:34:57

另一种方法是等待事件而不是使用计时器。

即为

    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();
        }
    }

您服务

    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();
        }

    }

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();
        }

    }
爱*していゐ 2024-11-14 00:34:57

设置 Timer.AutoReset = true。否则它只会完成一次工作。但最好在 Windows 服务中使用线程。

[编辑]
啊,是的。默认情况下自动重置为 true。我也将其放入我的代码中:
GC.KeepAlive( myTimer );
因此,如果它处于非活动状态,GC 不会将其删除。

Set Timer.AutoReset = true. otherwise it will do its work only one time. but it's better to work with threading in windows services.

[edit]
ah, yes. autoreset is true in default. I put this too in my code:
GC.KeepAlive( myTimer );
so the gc won't remove it if it is inactive.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文