Blog Archives

.Net Thread Safety

Any future posts regarding thread safe calls will go here.

C#: (field)

//place in an event handling label, on edit, click, update, etc
    if(label1.InvokeRequired)
    {
        label1.Invoke(new MethodInvoker(delegate 
{ 
label1.text = "value"; 
}
));
    }
else
{
label1.text = "value"; 
}

C#: (singleton)

public sealed class ObjType
{
    private static ObjType _ObjInstance;
    private static readonly object objlock;

    ObjType()
    {
_ObjInstance=null;
objlock = new object();
    }

    public static ObjType ObjInstance
    {
        get
        {
            lock (objlock)
            {
                if (_ObjInstance==null)
                {
                    _ObjInstance= new Singleton();
                }
                return _ObjInstance;
            }
        }
    }
}
Follow

Get every new post delivered to your Inbox.