zoukankan      html  css  js  c++  java
  • 异步或多线程支持一个任务执行完成

    贴一个简单的代码

    1:多线程

    public class Class1
        {
            public Class1()
            {
                Timer timer = new Timer();
                timer.Enabled = true;
                timer.Interval = 500;
                timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                Console.Read();
            }
            int n = 0;
            void timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                n++;
                System.Threading.Thread t = new System.Threading.Thread(
                    new System.Threading.ParameterizedThreadStart(newThread));
                t.Name = n.ToString();
                t.Start(n);
            }
            void newThread(object n)
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("n=" + n + " => i=" + i + "");
                    System.Threading.Thread.Sleep(1000);
                }
               Console.WriteLine("线程" + System.Threading.Thread.CurrentThread.Name + "结束");
               System.Threading.Thread.CurrentThread.Abort();
               System.Threading.Thread.CurrentThread.Join();
            }
        }

    2:委托

     public class Class2
        {
            public Class2()
            {
                Timer timer = new Timer();
                timer.Enabled = true;
                timer.Interval = 500;
                timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
                Console.Read();
            }
            int n = 0;
            void timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                n++;
                Working(n);
            }
            private delegate void DelegateReceiveData(int n);
            private void CallBackMethod(IAsyncResult ar)
            {
                try
                {
                    DelegateReceiveData dn = (DelegateReceiveData)ar.AsyncState;
                    dn.EndInvoke(ar);
                }
                catch (Exception ex)
                {
                   
                }
            }
            private void ReceiveData(int n)
            {
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine("n=" + n + " => i=" + i + "");
                    System.Threading.Thread.Sleep(1000);
                }
            }
            protected void Working(int n)
            {
                DelegateReceiveData dn = new DelegateReceiveData(ReceiveData);
                AsyncCallback acb = new AsyncCallback(CallBackMethod);
                IAsyncResult iar = dn.BeginInvoke(n, acb, dn);
            }
        }

    结果:

  • 相关阅读:
    mysql增加索引、删除索引、查看索引
    微信小程序跳转页面时参数过长导致参数丢失
    微信小程序:使用wx.request()请求后台接收不到参数
    微信小程序跳转web-vie时提示appId无法读取:Cannot read property 'appId' of undefined
    tomcat正常运行一段时间后,突然访问不了项目了
    注解@Async解决异步调用问题
    Linux之acl库的安装与使用(限制Linux某用户的访问权限)
    HashMap和Hashtable的详细区别
    如何处理MySQL经常出现CPU占用率达到99%
    IntelliJ IDEA 提交代码时出现:Code analysis failed with exception: com.intellij.psi......
  • 原文地址:https://www.cnblogs.com/KimhillZhang/p/3011980.html
Copyright © 2011-2022 走看看