zoukankan      html  css  js  c++  java
  • C#线程总结

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading;
    
    namespace Guoqingxunliang
    {
        class Program
        {
    
            public delegate int TakesAWhileDelegate(int data,int ms);
    
            static int TakesAWhile(int data, int ms)
            {
                Console.WriteLine("takesawhile started");
                Thread.Sleep(ms);
                Console.WriteLine("takesawhile completed");
                return ++data;
            }
    
            static void TakesAWhileCompleted(IAsyncResult iar)
            {
                if (iar == null) throw new ArgumentNullException("iar");
                TakesAWhileDelegate dl = iar.AsyncState as TakesAWhileDelegate;
                Trace.Assert(dl!=null,"Invalid object type");
                int result=dl.EndInvoke(iar);
                Console.WriteLine(result);
            }
    
    
            static void Main(string[] args)
            {
    
                #region 异步委托 
                
                TakesAWhileDelegate dl = TakesAWhile;
    
                //验证委托是否完成
                IAsyncResult iar = dl.BeginInvoke(1,300,null,null);
    
                while (!iar.IsCompleted)//只要委托没有完成其任务,程序的主线程就继续执行循环
                {
                    Console.WriteLine(".");
    
                    Thread.Sleep(50);
    
    
                }
    
                int result = dl.EndInvoke(iar);
                Console.WriteLine("result:{0}",result);
    
                #endregion 
    
                #region 句柄等待
    
                TakesAWhileDelegate dl_1 = TakesAWhile;
                IAsyncResult ar = dl_1.BeginInvoke(1,3000,null,null);
    
                while (true)
                {
                    Console.WriteLine(".");
                    if (ar.AsyncWaitHandle.WaitOne(50, false))
                    {
                        Console.WriteLine("can get the result now");
                        break;
    
                    }
                }
                int resu = dl_1.EndInvoke(ar);
                Console.WriteLine(resu);
    
    
                #endregion 
    
                #region 异步回调
    
                TakesAWhileDelegate dl3 = TakesAWhile;
    
                //使用回调方法,必须注意这个方法从委托线程中调用,而不是从主线程中调用
                ///最后一个参数,可以传递任意对象,以便从回调方法中访问它,传递委托实例很有用,
                ///这样回调方法就可以使用它获得异步方法的结果
                dl3.BeginInvoke(1,3000,TakesAWhileCompleted,dl3);
                for (int i = 0; i < 100; i++)
                {
                    Console.WriteLine(".");
                    Thread.Sleep(30);
    
                }
    
                #endregion 
    
                #region 线程
    
    
                #endregion 
    
            }
        }
    }
    

      参考:C#高级编程

  • 相关阅读:
    iOS下WebRTC音视频通话(二)-局域网内音视频通话
    Android初级教程短信防火墙
    iOS下WebRTC音视频通话(一)
    Android初级教程IP拨号器初识广播接受者
    Delphi常用字符串函数
    fastreport对象的属性和方法
    字段名、字段数不确定时,用 FastReport 动态生成报表
    动态创建Fastreport
    html 的 ContentType 小结
    ASP页面显示乱码解决方法/ASP设置编码
  • 原文地址:https://www.cnblogs.com/kuugachen/p/4005627.html
Copyright © 2011-2022 走看看