应该在什么时候,我按顺序处理我阻止,甚至 [英] Should I block even when I'm sequentially processing

查看:145
本文介绍了应该在什么时候,我按顺序处理我阻止,甚至的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有定期需要做一些工作,一个窗口服务。所以我成立了一个System.Timers.Timer的做到这一点。让我们假设它的可能的处理时间可能大于定时器间隔。让我们也认为这将是一个非常糟糕的事情,如果发生这种情况。

I have a windows service that periodically needs to do some work. So I set up a System.Timers.Timer to do this. Lets assume that its possible that the processing time could be greater than the timer interval. Lets also assume it would be a very bad thing if this happens.

要避免这种情况我设置计时器,以虚假的自动复位,然后在我的过程中调用start。

To avoid this I'm setting the AutoReset on the Timer to false and then calling start in my process.

public partial class Service : ServiceBase{

    System.Timers.Timer timer;


 public Service()
    {

    timer = new System.Timers.Timer();
    //When autoreset is True there are reentrancy problme 
    timer.AutoReset = false;


    timer.Elapsed += new System.Timers.ElapsedEventHandler(DoStuff);
}

 protected override void OnStart(string[] args)
 {

     timer.Interval = 1;
     timer.Start();

    }

 private void DoStuff(object sender, System.Timers.ElapsedEventArgs e)
 {

    Collection stuff = GetData();
    LastChecked = DateTime.Now;

    foreach (Object item in stuff)
    {
          item.Dosomthing(); //Do somthing should only be called once
     }     


    TimeSpan ts = DateTime.Now.Subtract(LastChecked);
    TimeSpan MaxWaitTime = TimeSpan.FromMinutes(5);


    if (MaxWaitTime.Subtract(ts).CompareTo(TimeSpan.Zero) > -1)
        timer.Interval = MaxWaitTime.Subtract(ts).TotalMilliseconds;
    else
        timer.Interval = 1;

    timer.Start();





 }

目前的code不会阻止,因为我知道,因为自动复位=假,其被顺序处理。但是,我能做到这一点安韦

Currently the code doesn't block because I know its being processed sequentially because of the AutoReset = false. But I could do it anway

lock(myLock)
{
    Collection stuff = GetData();
    LastChecked = DateTime.Now;

    foreach (Object item in stuff)
    {
          item.Dosomthing(); //Do somthing should only be called once
     }     

}

编辑:澄清我的问题

我做作的服务进行单线程,所以我并不需要一个锁。如果我添加我仍然在我的绩效预算锁因此性能不是一个理由不这样做。

I've contrived the service to be single threaded so I don't need a lock. If I add the lock I'm still inside my performance budget so performance isn't a reason not to.

基本上我权衡两面,我试图梳理出正确的事情™是什么。 在无锁的一面,我依靠诡计对我的code的正确性。在锁定身边,我会增加不必要的code。

Basically I'm weighing two sides and I'm trying to sort out what the Right Thing™ is. On the "No lock" side I'm relying on a contrivance for the correctness of my code. On the "Lock" side I would be adding unnecessary code.

哪家好?

推荐答案

我要么全力以赴线程安全还是没有线程安全的一切,只写你的文档中的很清楚,这个类不是线程安全的

I'd either go all-out thread safe or do no thread safety at all and just write in your documentation very clearly that the class isn't thread safe.

最糟糕的事情是回来了几年之后,而不是记住一切与否的安全。然后,你最终浪费了很多时间研究你的code,或者更糟,你最终会误导自己。

The worst thing is coming back a couple years later and not remembering whether or not everything's safe. Then you end up wasting a lot of time investigating your code, or worse, you end up misleading yourself.

这篇关于应该在什么时候,我按顺序处理我阻止,甚至的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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