如何用1分钟的计时器设计Windows服务? [英] How to design windows service with timer of 1 minute ?

查看:78
本文介绍了如何用1分钟的计时器设计Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在用c#设计windows服务。我设置了1分钟的时间间隔。

如何检查上一个进程是否在下次计时器启动时完成,以防止多次进入。

我想查看以前的服务流程如果该过程没有完成,我想跳过这个当前的过程。



我尝试过:



i设置

Hi I am designing the windows service in c#. I have set time interval of 1 minute.
How to check the previous process is completed on next timer start to prevent multiple entry.
I Want to check the previous service process if that process is not completed i want to skip this current process.

What I have tried:

i have set

public bool isComplete = true;

计时器我已经检查了



on timer i have checked

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    if (isComplete == true)
    {
        try
        {
            isComplete = false;
            eventLog1.WriteEntry("Service Initial Started at " + DateTime.Now.ToString() + Environment.NewLine);
            NexGenServiceRun();
            eventLog1.WriteEntry("Service Final Completed at " + DateTime.Now.ToString() + Environment.NewLine);
            isComplete = true;
        }
        catch
        {
            isComplete = true;
        }

    }
    else
    {
        eventLog1.WriteEntry("Service not started, Previous service is not completed " + Environment.NewLine );
    }
}

推荐答案

一般来说,你的想法是正确的。唯一的问题是你的isComplete变量可能存在竞争条件。不要像你那样进行检查/分配,而是使用 Interlocked.Exchange (T)方法(T,T)(System.Threading) [ ^ ]检查现有值,并在原子操作中将其设置为false。
Generally, your idea is right. The only issue would be a possible race condition on your isComplete variable. Instead of doing the checks/assign as you have done, use the Interlocked.Exchange(T) Method (T, T) (System.Threading)[^] to check the existing value, and to set it to false, in an atomic operation.


这篇关于如何用1分钟的计时器设计Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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