none
Lock statement of the method in DLL RRS feed

  • Question

  • I made a class which have common methods and declared locker object, Instance method and WriteLog method like following.

    public class CDMrCommon { private readonly object locker = new object();

        public string exePath;
        public string applicationpath;

        public void GetConfig()
        {
            exePath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
            applicationpath = System.IO.Path.GetDirectoryName(exePath);

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

        public void WriteLog(string strlog)
        {
            lock (locker)
            {
                try
                {
                    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);
                    }
                }
                catch
                {
                }
            }
        }

    }

    Two EXEs reference this DLL and call WriteLog() method to write any log like this way.

    GetConfig()//called at the begining of the process

    ..

    CDMrCommon.Instance.WriteLog("Some log");

    It works but I'm not sure if "private readonly object locker = new object();" is correct or not.

    Should it be static to ensure two different EXEs call the WriteLog() method.

    Can anybody give me some advice?


    • Edited by Jeff0803 Sunday, March 29, 2020 3:27 AM
    Sunday, March 29, 2020 3:20 AM

Answers

All replies

  • two processes don't use the same object.
    When you want to synchronize two processes, you need to use a named mutex.
    Sunday, March 29, 2020 7:13 AM
  • Hi Jeff0803,

    Thank you for posting here.

    For mutex usage, please see the following articles and examples.

    Mutex Class

    Threading with Mutex

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 30, 2020 3:13 AM