Click here to Skip to main content
15,891,033 members
Home / Discussions / C#
   

C#

 
QuestionIterating thru the Outlook mail folders - C# Pin
vjvivaj9-Nov-05 3:21
vjvivaj9-Nov-05 3:21 
AnswerRe: Iterating thru the Outlook mail folders - C# Pin
Judah Gabriel Himango9-Nov-05 5:24
sponsorJudah Gabriel Himango9-Nov-05 5:24 
GeneralRe: Iterating thru the Outlook mail folders - C# Pin
vjvivaj9-Nov-05 18:15
vjvivaj9-Nov-05 18:15 
Questionhelp with Deserialize a string problem in c# Pin
drey19-Nov-05 3:20
drey19-Nov-05 3:20 
AnswerRe: help with Deserialize a string problem in c# Pin
Jon Rista9-Nov-05 10:39
Jon Rista9-Nov-05 10:39 
NewsC# lock statement Pin
Den2Fly9-Nov-05 3:00
Den2Fly9-Nov-05 3:00 
AnswerRe: C# lock statement Pin
Judah Gabriel Himango9-Nov-05 5:20
sponsorJudah Gabriel Himango9-Nov-05 5:20 
GeneralRe: C# lock statement Pin
Jon Rista9-Nov-05 10:54
Jon Rista9-Nov-05 10:54 
I agree with Judah, locking 'this' or the class itself is very bad practice, and leads to a lot of problems. Standard practice is to use a semaphore, or a synchronization root object, as a lock target. Some simple examples of good locking can be represented by a synchronized collection, or the singleton object:

A singleton:
public class MySingleton
{
  #region Singleton Pattern
  private static volatile MySingleton m_instance;
  private static object m_syncRoot = new object(); // lock target

  public static MySingleton
  {
    get
    {
      if (m_instance == null)
      {
        // Lock the syncroot object, rather than typeof(MySingleton)
        lock (m_syncRoot)
        {
          if (m_instance == null)
          {
            m_instance = new MySingleton();
          }
        }
      }

      return m_instance;
    }
  }
  #endregion
}


Synchronizing a collection:
public class MyCollection: CollectionBase
{
  private class MySyncCollection: MyCollection
  {
    internal MySyncCollection(MyCollection col)
    {
      m_col = col;
      m_root = col.SyncRoot;
    }

    private MyCollection m_col;
    private object m_root;

    public override object SyncRoot
    {
      get
      {
        return m_root;
      }
    }

    public int Add(object item)
    {
      int index;
      lock (m_root)
      {
        index = m_col.Add(item);
      }
      return index;
    }

    // ... Override other methods ...
  }

  private object m_syncRoot = new object();  // lock target

  public override object SyncRoot
  {
    get
    {
      m_syncRoot;
    }
  }

  public int Add(object item)
  {
    return List.Add(item);
  }

  public static MyCollection Synchronized(MyCollection col)
  {
    if (col == null)
      throw new ArgumentNullException("The specified collection is null. Can not synchronize a non-existent collection.");

    return new MyCollection.MySyncCollection(col);
  }

  // ... Other methods ...
}

NewsRe: C# lock statement Pin
Den2Fly9-Nov-05 13:45
Den2Fly9-Nov-05 13:45 
QuestionFTP Pin
v.k.s9-Nov-05 2:48
v.k.s9-Nov-05 2:48 
AnswerRe: FTP Pin
lmoelleb9-Nov-05 20:57
lmoelleb9-Nov-05 20:57 
QuestionC# control licence question Pin
dojdoj9-Nov-05 2:47
dojdoj9-Nov-05 2:47 
Questionhow to manipulate exe files ? Pin
hdv2129-Nov-05 2:09
hdv2129-Nov-05 2:09 
AnswerRe: how to manipulate exe files ? Pin
User 66589-Nov-05 3:48
User 66589-Nov-05 3:48 
QuestionC# and IUnknown (COM) Pin
MLS9-Nov-05 1:22
MLS9-Nov-05 1:22 
QuestionRecursive function to return the max key of nodes of a singly connected list? Pin
ektoras9-Nov-05 0:35
ektoras9-Nov-05 0:35 
QuestionRecursive function to display the number of nodes of a singly connected list? Pin
ektoras9-Nov-05 0:31
ektoras9-Nov-05 0:31 
AnswerRe: Recursive function to display the number of nodes of a singly connected list? Pin
J4amieC9-Nov-05 2:16
J4amieC9-Nov-05 2:16 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
ektoras9-Nov-05 3:36
ektoras9-Nov-05 3:36 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
Wjousts9-Nov-05 4:18
Wjousts9-Nov-05 4:18 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
ektoras9-Nov-05 7:20
ektoras9-Nov-05 7:20 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
Wjousts9-Nov-05 7:35
Wjousts9-Nov-05 7:35 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
J4amieC9-Nov-05 4:45
J4amieC9-Nov-05 4:45 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
ektoras9-Nov-05 7:38
ektoras9-Nov-05 7:38 
GeneralRe: Recursive function to display the number of nodes of a singly connected list? Pin
J4amieC9-Nov-05 9:27
J4amieC9-Nov-05 9:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.