ms-help://MS.NETFrameworkSDK.CHS/csspec/html/vclrfcsharpspec_8_12.htm
下列形式的 lock 语句lock (x) ...
(其中 x 是引用类型的表达式)完全等效于System.Threading.Monitor.Enter(x);
try {
   ...
}
finally {
   System.Threading.Monitor.Exit(x);
}
不同的是 x 只被计算一次。
按照上面的说法,异常以后应该释放临界区了

解决方案 »

  1.   

    能。using System;
    using System.Threading;
    public class Test
    {
    public  void M1()
    {
          Run("M1");
    }
    public  void M2()
    {
      Run("M2");
    }

    private int i = 0;
    private int t = 0;
    public void Run(string name)
    {
    lock(this)
    {
        for (i = t; i <100; i++)
        {
         t = i;
        Thread.Sleep(100);
        if (i == 50)
        {
         t++;
               throw new Exception(name);
         }
         Console.WriteLine(name + " " + i.ToString());
         }

    }
    }
    }
    public class EntryPoint
    {
    public  static void Main()
    {
    Test t = new Test();
    Thread t1 = new Thread(new ThreadStart(t.M1));
    Thread t2 = new Thread(new ThreadStart(t.M2));
    t2.Start();
    t1.Start();
    }
    }
      

  2.   

    try
    {
    //lock
    }
    catch(Exception ee)
    {}
    finally
    {
    //unlock
    }
    这样不得了吗?