static void Main() { Gut <string> gut = new Gut <string>(); Dosomething <string> dosomething = new Dosomething <string>(gut.Tag); Publisher <string> publisher = new Publisher <string>(); publisher.Reminder += dosomething; publisher.Show("Wie geht es ihnen?"); }
static void Main(string[] args) { Calc calc = (a, b) => a + b; Dosomething dosomething = () => { WriteLine("Dosomthing"); }; WriteLine(calc(3, 4)); dosomething(); }
public DeleAndThread() { Console.WriteLine("****************************委托执行测试*****************************************"); var action2 = new Action<string, int>(Dosomething1); action2("我是带参数的委托Action", 1); var action3 = new Action(() => Dosomething1("", 0)); Expression<Func<int, int, int, int>> expr = (x, y, z) => (x + y) / z; Console.WriteLine($"表达式Expression<Func<int, int, int, int>>执行结果:{expr.Compile()(1, 2, 3)}"); Dosomething method1 = t => Console.WriteLine($"我是实现委托的方法,{t}"); method1.Invoke("delegate"); Task<int> Act(int i) => new Asynctest().LoadCache(i); Task<int> result = Act(10); Console.WriteLine("************************异步委托*****************************"); result.ContinueWith(t => Console.WriteLine("异步调用完成后的最终返回计算结果为:{0}", t.Result.ToString())); Func<string, string> method2 = GetSomething2; string result1 = method2("xuesky"); //以下Core不支持 var s1 = method2.BeginInvoke("xuesky", t => Console.WriteLine($"异步回调,当前线程{Thread.CurrentThread.ManagedThreadId}"), "age"); //var result2 = method2.EndInvoke(s1); //Console.WriteLine($"当前线程{Thread.CurrentThread.ManagedThreadId},异步返回值为{result1}"); //以下Core不支持 Func<string, int, (string, int)> method = GetSomething; //var ss = method.BeginInvoke("xuesky", 1, t => Console.WriteLine($"异步回调,当前线程{Task.CurrentId}"), "age"); Console.WriteLine("************************多线程*****************************"); Task task = new Task(t => { Console.WriteLine($"我是多线程:{t},线程ID为:{Thread.CurrentThread.ManagedThreadId}"); }, 5); task.Start(); var t1 = Task.Factory.StartNew(DoSomething); var t2 = Task.Factory.StartNew(DoSomething); var t3 = Task.Factory.StartNew(DoSomething); var t4 = Task.Factory.StartNew(DoSomething); var t5 = Task.Factory.StartNew(DoSomething); var t6 = Task.Factory.StartNew(DoSomething); Console.WriteLine("是否主动释放DbContext(y/n)"); var yes = Console.ReadLine(); Console.WriteLine("请输入模拟并发量"); var number = Console.ReadLine(); SemaphoreSlim sem = new SemaphoreSlim(int.Parse(number)); var j = 0; while (j < 1000) { Console.WriteLine("启动第" + j++ + "个线程"); sem.Wait(); var t7 = new TaskFactory().StartNew(() => { Console.WriteLine($"多线程执行,线程ID:{Thread.CurrentThread.ManagedThreadId}"); }); } }
static void Main() { TrafficLight <int> trafficlight = new TrafficLight <int>(); Dosomething <int> dosomething = new Dosomething <int>(trafficlight.Show); dosomething(4); TrafficLight <string> trafficLight1 = new TrafficLight <string>(); Dosomething <string> dosomething1 = new Dosomething <string>(trafficLight1.Show); dosomething1("Hello world"); }