首页 > 代码库 > c#同步锁Monitor.Enter(T)

c#同步锁Monitor.Enter(T)

protected static object MObjLock = new object();//同步锁        public string GetData(int mId)        {            Monitor.Enter(MObjLock);//lock            string data = http://www.mamicode.com/string.Empty;            try            {                 //你的代码                
            }            catch (Exception err)            {                Log4NetHelper.WriteError(err);            }            finally            {                Monitor.Exit(MObjLock);//unlock            }            return data;        }

Monitor.Enter(T)上锁后,其它代码调用此方法要等到Monitor.Exit(T)之后,适用于抽奖等

c#同步锁Monitor.Enter(T)