排他锁Lock

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        private static object WeixinPushHelperLock = new object();
        static void Main(string[] args)
        {
            for (int i = 0; i < 10; i++)//重复执行的方法
            {
                NewMethod();
            }
        }

        private static void NewMethod()
        {
            if (Monitor.IsEntered(WeixinPushHelperLock)) return;//锁定中就return
            Monitor.Enter(WeixinPushHelperLock);
            //你的业务代码。。。

            Monitor.Exit(WeixinPushHelperLock);//执行完成后释放
        }
    }
}

 

posted @ 2017-01-09 09:59  chenjingchun  阅读(291)  评论(0编辑  收藏  举报