Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So,
Carrying on from my last question on this subject, I thought I had it all licked. All is working fine; I have implemented a locker to stop simultanious access of the disk. I was aware at the time that I had two instances of my object running in their own threads but I now require there to be only one. I will explain..
Here is my useful class that requires to run along in it's own space; I have applied the locker where methods access the disk:-
C#
public class Logger
{
    //Threadlocker
    static readonly object locker = new object();
    .....
}


Now, instantiate it from class x
C#
_logger = new Logger();
loggerThread = new System.Threading.Thread(new System.Threading.ThreadStart(_logger.Initialise));
loggerThread.Start();


..and again in class y
C#
_logger = new Logger();
loggerThread = new System.Threading.Thread(new System.Threading.ThreadStart(_logger.Initialise));
loggerThread.Start();


All is ok, I use it's properties from each calling class to log to my file, each class identifies who is writing to the file and so no problem. I would however like to maintain the variables from all calling classes so that these are all logged regardless of who is calling the 'log' method..
ie. I would like to instantiate it in one place but have multiple classes able to use it. I'm sure the solution will be obvious to many of you but not me i'm affraid..
Posted
Comments
tumbledDown2earth 17-Apr-13 7:33am    
Did you hear about the Singleton Pattern? You can find a hint here: http://www.dofactory.com/Patterns/PatternSingleton.aspx

1 solution

C#
public class Logger
{
    //Threadlocker
    static readonly object locker = new object();
    static Logger logger = new Logger();
    
    private Logger()
    {
       _logger = new Logger();
loggerThread = new System.Threading.Thread(new System.Threading.ThreadStart(_logger.Initialise));
loggerThread.Start();
    }

    public static Logger CurrentLogger
    {
         get
         {
            return logger;
         }
    }

    ....
}
 
Share this answer
 
Comments
Member 9862872 17-Apr-13 11:33am    
How do instantiate it without it entering an infinate loop ?
Member 9862872 18-Apr-13 5:38am    
Many thanks tumbledDown2earth, I have implemented your solution (at least similar) and it works great. I guess you hadn't intended the "new Logger / new Thread" to be in the constructor; I had a look at the singleton pattern link you provided and it all become clear. I did it like this:
public static Logger CurrentLogger()
{
if (_logger == null)
{
lock (locker)
{
if (_logger == null)
{
_logger = new Logger();
Thread loggerThread = new System.Threading.Thread(new System.Threading.ThreadStart(_logger.Initialise));
loggerThread.Start();
}
}
}
return _logger;
}
tumbledDown2earth 18-Apr-13 5:47am    
neat :) ... good luck with ur project

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900