• 高级编程 实验代码


    #region
    //List<Mouse> list = new List<Mouse>();
    //for (int i = 0; i < 10; i++)
    //{
    // list.Add(new Mouse() { Name = i.ToString(), Money = i * 10 });
    //}
    //foreach (var i in list)
    // Console.WriteLine(i.Name);

    //ILookup<string, int> lookup = list.ToLookup(p => p.Name, p => p.Money);
    //foreach (var l in lookup)
    //{
    // Console.WriteLine(l.Key + "-------->");
    // foreach (var k in l)
    // Console.WriteLine(k);

    //}
    //Stack<string> str = new Stack<string>();
    //for (int i = 0; i < 100; i++)
    //{
    // str.Push(i.ToString());
    //}
    // //A:
    //Console.WriteLine(str.Count + "---->");
    //while(str.Count>0)
    //{
    // Console.WriteLine(str.Pop());
    //}
    //if(null!==str && str.Count>0)
    //goto A;

    //SortedList
    #endregion

    #region
    //Type type = Type.GetType("ConsoleApplication1.Mouse");
    //System.Reflection.MemberInfo[] infoList = type.GetMembers();
    //if (null == infoList)
    // Console.WriteLine("null");
    //else
    //{
    // foreach (System.Reflection.MemberInfo info in infoList)
    // {
    // Console.WriteLine(info.Name);
    // try
    // {
    // Console.WriteLine("try");
    // if (info.MemberType == System.Reflection.MemberTypes.Method)
    // {
    // //type.InvokeMember(info.Name, System.Reflection.BindingFlags.Public,System.Type.DefaultBinder, type, new object[] { "chen" });
    // type.InvokeMember(info.Name, System.Reflection.BindingFlags.Public, System.Type.DefaultBinder, type, new object[] { "chen" });
    // }
    // }
    // catch
    // {
    // Console.WriteLine("catch");
    // }
    // finally
    // {
    // Console.WriteLine("Finally");
    // }
    // break;
    // }
    //}
    #endregion

    #region
    //try
    //{
    // throw new MyException("haha");
    //}
    //catch (MyException ex)
    //{
    // Console.WriteLine(ex.Message);
    // Console.WriteLine(ex.Source);
    //}
    //finally
    //{
    // Console.WriteLine("执行了Finally");
    //}
    #endregion

    #region
    /*
    using (CSharpCodeProvider sharp = new CSharpCodeProvider())
    {
    CompilerParameters compiler=new CompilerParameters();
    compiler.GenerateExecutable=true;
    StringBuilder str=new StringBuilder();
    str.Append("using System;");
    str.Append("namespace A ");
    str.Append("{");
    str.Append("class P");
    str.Append("{");
    str.Append(" static void Main()");
    str.Append("{}");
    str.Append("public static void Run(){");
    str.Append("Console.WriteLine(DateTime.Now.ToString()+\"--------->此为手动编译\");");
    str.Append("}");
    str.Append("}");
    str.Append("}");


    var result = sharp.CompileAssemblyFromSource(compiler, str.ToString());
    Console.WriteLine(result.Output);
    Console.WriteLine(result.PathToAssembly);
    if (!result.Errors.HasErrors)
    {
    Type t = result.CompiledAssembly.GetType("A.P");
    Console.WriteLine("开始调用..");
    Console.WriteLine("----->");
    t.InvokeMember("Run", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, null);
    }
    else
    {
    foreach (CompilerErrorCollection error in result.Errors)
    Console.WriteLine(error.ToString());
    }
    }
    */
    #endregion

    #region
    /*
    Console.WriteLine(System.AppDomain.CurrentDomain.FriendlyName);
    System.AppDomain newAppDomain = AppDomain.CreateDomain("NewAppDomain");
    newAppDomain.ExecuteAssembly(@"G:\练手\Test\Test\bin\Debug\test.exe");
    */
    #endregion

    #region
    /*
    SpeakingDelegate sp = Speaking;
    IAsyncResult ar = sp.BeginInvoke("haha", (p) => { Console.WriteLine("OK"); }, null);
    while (!ar.IsCompleted)
    {
    Console.Write(".");
    System.Threading.Thread.Sleep(2000);
    }
    //Console.Write("OK");
    //sp.EndInvoke(ar);
    */
    #endregion

    #region 线程池...
    /*
    int maxThreads=0;
    int actionThreads=0;

    ThreadPool.GetMaxThreads(out maxThreads, out actionThreads);
    Console.WriteLine(string.Format("maxThreads:{0},actionThreads:{1}", maxThreads, actionThreads));
    ThreadPool.QueueUserWorkItem(p => {
    for (int i = 0; i < 100; i++)
    {
    Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
    System.Threading.Thread.Sleep(100);
    }
    });
    System.Threading.Thread.Sleep(10000);
    */
    #endregion

    #region
    /*
    log4net.Config.XmlConfigurator.Configure();
    Program pro = new Program();
    pro.log.Error("Warn>>>>>>>>>>>>>>");
    pro.log.Debug("Debug>>>>>>>>>>>>>>");
    */
    #endregion


    #region Monitor
    /*
    object obj = new object();
    Func<string> f = () =>
    {
    if (Monitor.TryEnter(obj, 500))
    {
    System.Threading.Thread.Sleep(2000);
    Monitor.Exit(obj);
    return "执行没有超时";
    }
    else
    {
    return "执行超时";
    }
    };
    Thread td1 = new Thread(() => { Console.WriteLine(f()); });
    td1.Start();
    Thread td2 = new Thread(() => { Console.WriteLine(f()); });
    td2.Start();
    */
    #endregion

    #region 异步委托
    /*
    Action<string> a = (p) => { Console.WriteLine(p); };
    a.BeginInvoke("gaga", p => { if (p.IsCompleted)Console.WriteLine("OK"); else Console.WriteLine("NO");
    Console.WriteLine(p.AsyncState.ToString()); p.AsyncWaitHandle.WaitOne(1000, true);
    }, "zz");
    Console.ReadLine();
    */
    #endregion

    #region Mutex
    //bool notCreateProduct;
    //Mutex mu = new Mutex(false, "Programe", out notCreateProduct);
    //if (!notCreateProduct)
    //{
    // Console.Write("已经有实例在运行,等待..");
    // // System.Threading.Thread.Sleep(2000);
    // }
    //else
    //{
    // Console.Write("以运行....");
    // Console.ReadLine();
    //}
    #endregion

    #region Mutex
    /*
    * 控制某一区域只能同时只有一个线程使用
    */
    /*
    Mutex mu = new Mutex();
    Action a = () => {
    mu.WaitOne();
    Console.WriteLine("{0} 线程已经进入", System.Threading.Thread.CurrentThread.Name);
    System.Threading.Thread.Sleep(2000);
    mu.ReleaseMutex();
    };
    for (int i = 0; i < 100; i++)
    {
    Thread td = new Thread(new ThreadStart(a));
    td.Name = (i + 1).ToString();
    td.Start();
    }
    Console.ReadLine();
    */
    #endregion


    #region Semaphore
    /*
    * Semaphore 限制同时执行某一代码块最多线程数
    *
    */
    /*
    Semaphore sem = new Semaphore(2,2);
    Action ac = () =>
    {
    Console.WriteLine("{0}等待...",Thread.CurrentThread.Name);
    sem.WaitOne();
    Console.WriteLine("{0}进入...", Thread.CurrentThread.Name);
    Thread.Sleep(3000);
    Console.WriteLine("{0}推出...", Thread.CurrentThread.Name);
    sem.Release();

    };

    for (int i = 0; i < 5; i++)
    {
    Thread td = new Thread(new ThreadStart(ac));
    td.Name = (i + 1).ToString();
    td.Start();
    }
    */
    #endregion


    #region
    /*
    * ManualResetEvent
    * AutoResetEvent
    * 两个使用差不多,都是时事件执行等待,设置Set()方法,使等待线程继续
    * 不同是ManualResetEvent 设置Set()后,等待的多个线程检同时执行下去
    * 而autoResetEven则是一个一个执行,并且第一个执行后,将设置autoResetEven 为Reset() ,
    * 也就是说第一个线程执行后,必须再次设置Set(),才能执行第二个,一次类推
    * 而第一次执行的线程优先级是等待线程种最高
    */
    #region ManualResetEvent

    //ManualResetEvent mre = new ManualResetEvent(false);
    //Action trmain = () =>
    //{
    // Thread tr = Thread.CurrentThread;
    // Console.WriteLine("thread: waiting for an event");
    // mre.WaitOne();
    // Console.WriteLine("thread: got an event");
    // for (int x = 0; x < 10; x++)
    // {
    // Thread.Sleep(1000);
    // Console.WriteLine(tr.Name + ": " + x);
    // }
    //};

    //Thread thrd1 = new Thread(new ThreadStart(trmain));
    //thrd1.Name = "thread1";
    //thrd1.Start();
    //for (int x = 0; x < 10; x++)
    //{
    // Thread.Sleep(900);
    // Console.WriteLine("Main:" + x);
    // if (5 == x) mre.Set();
    //}
    //while (thrd1.IsAlive)
    //{
    // Thread.Sleep(1000);
    // Console.WriteLine("Main: waiting for thread to stop");
    //}
    #region
    // ManualResetEvent mre = new ManualResetEvent(false);

    //AutoResetEvent mre = new AutoResetEvent(false);
    //Action act = () =>
    //{
    // Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ":等待...");
    // mre.WaitOne();
    // Console.WriteLine(System.Threading.Thread.CurrentThread.Name + ":执行完成!");
    //};
    //for (int i = 0; i < 10; i++)
    //{
    // Thread td = new Thread(new ThreadStart(act));
    // td.Name=(i+1).ToString();
    // td.Start();
    // System.Threading.Thread.Sleep(400);
    // if (i == 9)
    // {
    // Console.WriteLine("开始执行所有");
    // mre.Set();
    // }

    //}
    #endregion
    #endregion

    #region AutoResetEvent

    //AutoResetEvent auto = new AutoResetEvent(false);
    //Action fun = () =>
    //{
    // Console.WriteLine("{0}等待", Thread.CurrentThread.Name);
    // auto.WaitOne();
    // Console.WriteLine("{0}进入", Thread.CurrentThread.Name);
    //};

    //for (int i = 0; i < 10; i++)
    //{
    // Thread td = new Thread(new ThreadStart(fun));
    // td.Name = (i + 1).ToString();
    // td.Start();
    // if (i == 5)
    // {
    // auto.Reset();
    // }
    //}
    //Thread.Sleep(2000);
    //for (int i = 0; i < 10; i++)
    //{
    // auto.Set();
    // Thread.Sleep(100);
    //}
    //Console.ReadLine();

    #endregion

    #endregion
    //ReaderWriterLockSlim reader = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);

    #region Timers
    /*
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 1000;
    timer.Elapsed += new System.Timers.ElapsedEventHandler((p, k) => Console.WriteLine(DateTime.Now.Second));
    timer.AutoReset = false ;
    timer.Start();
    Console.Read();
    */
    #endregion
  • 相关阅读:
    如何在android项目中引用project作为类库引用
    Unity3d之MonoBehaviour的可重写函数整理
    Phonegap hello world 不容易啊~!
    数据结构,到底如何用中学,学中用?
    自动化测试(1610)
    软件测试人员的发展路线
    软件测试分类
    我的第一篇博客随笔
    安装虚拟机和Linux系统的学习
    英雄联盟的游戏经验
  • 原文地址:https://www.cnblogs.com/blackman/p/2082214.html
Copyright © 2020-2023  润新知