Don't use System.Threading.Timer to synchronize?

五迷三道 提交于 2019-12-11 01:07:03

问题


I have some code looking like this:

using System.Threading;

public sealed class MyClass : IDisposable {
    private readonly Timer _timer;

   public MyClass() {
        _timer = new Timer(MyCallback);
   }

   public void SomeMethod() {
       lock (_timer) {
           ...
       }
   }

   ...

}

When I run code analysis (FxCop) I get the error message
CA2002 : Microsoft.Reliability : 'MyClass.SomeMethod' locks on a reference of type 'Timer'. Replace this with a lock against an object with strong-identity.

I found some documentation here.

An object is said to have a weak identity when it can be directly accessed across application domain boundaries.

How can my private timer object be accessed across appdomains? The only reason I can think of is if the Timer class wraps some global system handle but I don't understand how that handle is involved in the locking.

There is a list of weak classes on the documentation page but Timer is not mentioned. MemomoryStream is also not on the list but it is included in the examples. How can a local MemoryStream object be weak?


回答1:


Timer extends MarshalByRefObject, which is why it's complaining, I suspect.

Personally I would recommend only locking on a private object which only your code can know about:

using System.Threading;

public sealed class MyClass : IDisposable {
    private readonly Timer _timer;
    private readonly object _mutex = new object();

   public MyClass() {
        _timer = new Timer(MyCallback);
   }

   public void SomeMethod() {
       lock (_mutex) {
           ...
       }
   }

   ...
}

That way you don't need to worry about whether Timer decides to lock on this etc.




回答2:


You can't be sure that Timer doesn't lock itself on its instance. Its best to use just a new object().



来源:https://stackoverflow.com/questions/8942104/dont-use-system-threading-timer-to-synchronize

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!