2
if (runInDemoMode)
{

    lock (this)
    {
        //Initalization of tables
        dCreator.createInitialTables();
        SetupPlugins();
        AutoConfigure(database);

        //Simulator                   
        sim.processSimulatedData();
    }
    return;

}

Ideally i would like to have tables initialized (once) and then the simulator running again and again. Since I have the 3 inital methods for tables also under lock this they keep getting initalized again and again which is not how i want it.

Any Suggestions as to how i can ensure that if i am in run demo mode i can initalize the tables once and then run simulator again and again.

It is vital the tables are initalized before the simulator runs or else it will not work.

pattivacek
  • 5,617
  • 5
  • 48
  • 62
  • 4
    You should lock on a private object, not `this` (the common pattern is `private readonly object _createTableLockObj = new object();`). If another class did `lock(yourClass)` it would prevent your code from entering your lock too. By using a private object to lock on the lock is scoped to only your single function. – Scott Chamberlain Jul 24 '13 at 13:37
  • 4
    [Don’t lock on `this`](http://stackoverflow.com/q/251391/1968) – Konrad Rudolph Jul 24 '13 at 13:37
  • 1
    Create a static volatile bool that is set to true once you are initialized. Only if this bool is false, call the table initialization routine – Ganesh R. Jul 24 '13 at 13:40
  • Is this a multithreaded application? (You haven't mentioned threads or elaborated on your design at all). The lock statement lets the current thread lock a critical section of code (see http://msdn.microsoft.com/en-us/library/c5kehkcz(v=vs.71).aspx ) – Dan Esparza Jul 24 '13 at 13:41

4 Answers4

2

Using a lock here is perhaps not the best idea. You should consider just having a variable named initialized that is a boolean value set initially to false. If it is set to false when this code is reached, run the initialization block of code and set the variable to true. Then the next time this code is reached your branch will evaluate to false and the initialization code will not run again.

If you are implying that these runs are not within one application call but spread across individual invocations of your application in series, you may have to write this variable to a file the first time and then read it back in future runs.

pattivacek
  • 5,617
  • 5
  • 48
  • 62
1

You're probably looking for Double-Check Locking.

private static readonly object _locker = new object();

public SomeClass(bool runInDemoMode)
{
    if (runInDemoMode)
    {
        lock (_locker)
        {
            if (runInDemoMode)
            {
                //Initalization of tables
                dCreator.createInitialTables();
                SetupPlugins();
                AutoConfigure(database);

                //Simulator                   
                sim.processSimulatedData();
            }
        }
    }
}

It would probably be better to abstract this logic into some DemoInitializer class that can independently setup and teardown the application.

Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
0

Others have noted the issue with your lock pattern and this is difficult to answer without seeing the rest of your code, but you could easily add a bool to help with this.

bool tablesInitialized = false;

if (runInDemoMode)
{

    lock (this)
    {
        if (!tablesInitialized)
        {
            //Initalization of tables
            dCreator.createInitialTables();
            SetupPlugins();
            AutoConfigure(database);

            tablesInitialized = true;
        }

        //Simulator                   
        sim.processSimulatedData();
    }
    return;

}
wilso132
  • 829
  • 5
  • 10
0

lock will only keep the code within it from being executed on multiple threads at the same time.

What you need is an initialization variable within your class or at whatever level is appropriate (it's unclear so far where you need it) that lets your code know it's already initialized.

bool isInitialized = false;

...

if (runInDemoMode)
{

    if (!isInitialized)
    {
        //Initalization of tables
        dCreator.createInitialTables();
        SetupPlugins();
        AutoConfigure(database);

        //Simulator                   
        sim.processSimulatedData();

        isInitialized = true;
    }
    return;

}
Kevin
  • 4,586
  • 23
  • 35