8

The following code block, performs loading of an object in C#.

public bool IsModelLoaded { get; set; }
public override MyObject Load()
{
    if (!IsModelLoaded)
    {
        Model = MyService.LoadMyObject(Model);
        IsModelLoaded = true;
    }
    return Model;
}

My intention is to run this block only once, and hence loading the Model only once. Nevertheless, this code block runs twice from 2 different threads.

How can I make sure that this block runs only once? (on multiple threads).

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
KayzerSoze
  • 93
  • 1
  • 10

7 Answers7

4

Use the Lazy<T> Class:

private readonly Lazy<MyObject> myObject;

public MyClass()
{
    myObject = new Lazy<MyObject>(() =>
    {
        return MyService.LoadMyObject();
    }, LazyThreadSafetyMode.ExecutionAndPublication);
}

public bool IsModelLoaded
{
    get { return myObject.IsValueCreated; }
}

public override MyObject Load()
{
    return myObject.Value;
}
dtb
  • 213,145
  • 36
  • 401
  • 431
2

Simplest would be to add

[MethodImpl(MethodImplOptions.Synchronized)]
public override MyObject Load()
{
   //snip
}

but be aware this puts a lock on the entire object, not just the method. Not really great practice.

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions.aspx

Synchronized

Specifies that the method can be executed by only one thread at a time. Static methods lock on the type, whereas instance methods lock on the instance. Only one thread can execute in any of the instance functions, and only one thread can execute in any of a class's static functions.

Adrian
  • 453
  • 8
  • 18
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103
2

I are trying to implement singleton pattern. But your version is not thread safe. Read more here: http://www.dofactory.com/Patterns/PatternSingleton.aspx. Try to use this implementation:

public sealed class Singleton
{
    static Singleton instance=null;
    static readonly object padlock = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance==null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}
Artem Vyshniakov
  • 16,355
  • 3
  • 43
  • 47
2

If you want to write thread safe code and make sure that the block runs only once, you can write like this:

private System.Object lockThis = new System.Object(); 
public override MyObject Load()
{
    lock (lockThis) {
        if (!IsModelLoaded)
        {
            Model = MyService.LoadMyObject(Model);
            IsModelLoaded = true;
        }
    }
    return Model;
}
Roozbeh Zabihollahi
  • 7,207
  • 45
  • 39
1
Action myCodeBlock = ()=>
{
  //do your job
  //...
  myCodeBlock = ()=>{};
}

After calling myCodeBlock() once it will be rewritten by method that does nothing. You still need to make sure this metod is called safely - use lock or whatever.

Anri
  • 6,175
  • 3
  • 37
  • 61
1

You can use lock Statement (C# Reference)

atredis
  • 372
  • 2
  • 10
-1

Create a static object (like a boolean) that determines if the code has ran or not by putting it in an if statement :)

EDIT: I'm not sure if this is threadsafe, so it might not be the solution for you.

Graknol
  • 55
  • 1
  • 8