/// <summary> /// /// </summary> /// <returns></returns> private static WillLogger Get() { lock (objLock) { if (logger == null) { logger = new WillLogger(); } return(logger); } }
static void Main(string[] args) { string s = "上面的代码会在编译的时候出错: Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type. 方法要求参数的是一个委托(delegate)类型, 而现在传递的只是一个匿名方法. 产生这个错误最根本的原因是编译器在处理匿名方法的时候, 没法推断出这个委托的方法返回的是什么类型, 也就不知道返回一个什么样的委托"; Random random = new Random(); DateTime start = DateTime.Now; Parallel.For(0, 100, e => { for (int j = 0; j < 1000; j++) { WillLogger.Info(j + s); Thread.Sleep(random.Next(10) * 10); } }); DateTime end = DateTime.Now; TimeSpan span = end.Subtract(start); Console.WriteLine(string.Format("运行了{0}分{1}秒", span.Minutes, span.Seconds)); //List<string> list = new List<string>(); //new Thread(delegate () //{ // while (true) // { // if (list.Count == 0) // continue; // int tempI = (list.Count-1) / 2; // list.RemoveRange(0, tempI); // Thread.Sleep(1); // } //}).Start(); //try //{ // for (int j = 0; j < 100000000; j++) // { // list.Add(j+""); // Thread.Sleep(1); // } //} //catch (Exception) //{ // throw; //} }