Ever wondered what the difference is…

… between using a lock statement, versus using a Montior.Enter / Monitor.Exit clause? Actually there’s a few, but the main one is that a lock statement is inherently thread safe, where-as a Montior is not. So -


    try {
        Monitor.Enter(_lock);
        DoThreadSafeCode();

    } finally {
        Monitor.Exit(_lock);
    }

is essentially the same as -


    lock (_lock) {
        DoThreadSafeCode();
    }
Posted in .Net at December 4th, 2007. No Comments.