none
lock statement RRS feed

  • Question

  • I have two questions regarding lock statement.

    I write log to the text file like following.

    private readonly object locker = new object(); . . . public void WriteLog(string strlog) { lock (locker) { using (FileStream file = new FileStream(applicationpath + @"\Log\" + DateTime.Now.Date.ToString("MMddyyyy") + ".log", FileMode.Append, FileAccess.Write, FileShare.Read)) using (StreamWriter w = new StreamWriter(file, Encoding.Unicode)) { w.WriteLine(DateTime.Now.ToLongTimeString() + "\t" + strlog); } } }

    1. Somebody says the object used in the lock statement should be static readonly but MSDN shows readonly. which one is correct?

    2. Is that all? Don't I have to release lock in any way? 




    • Edited by Jeff0803 Saturday, March 14, 2020 3:39 AM
    Saturday, March 14, 2020 3:32 AM

Answers

  • 1. The object in the lock statement only needs to be static if the function is static, or if you are trying to synchronize access among ALL instances of the class.

    2. No. the lock is released automatically when you leave the "lock" block.  That's the beauty of that statement.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Saturday, March 14, 2020 5:12 AM

All replies

  • 1. The object in the lock statement only needs to be static if the function is static, or if you are trying to synchronize access among ALL instances of the class.

    2. No. the lock is released automatically when you leave the "lock" block.  That's the beauty of that statement.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Saturday, March 14, 2020 5:12 AM
  • 1. The object in the lock statement only needs to be static if the function is static, or if you are trying to synchronize access among ALL instances of the class.

    The WriteLog() is not static method but is called through Instance like following.

    public static CDMrCommon Instance
    {
        get
        {
            if (m_CDMrCommon != null)
            {
                return m_CDMrCommon;
            }
            else
            {
                m_CDMrCommon = new CDMrCommon();
                return m_CDMrCommon;
            }
        }
    }

    And called like following.

    CDMrCommon.Instance.WriteLog(".....");
    In this case, use static keyword?



    • Edited by Jeff0803 Saturday, March 14, 2020 4:11 PM
    Saturday, March 14, 2020 4:09 PM
  • I'm going to be sorry I went into this much detail, but here goes.

    Technically speaking, in this specific case it doesn't matter.  What you have there is a "singleton" -- a class that will only have one instance.  Thus, whether the lock is a static that exists outside that one instance or a member that exists within the instance will not change things.

    Having said that, however, I stand by my initial statement.  Since the function using the lock is not a static, the lock should not be a static.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Monday, March 16, 2020 4:33 AM