欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

等待句柄判断异步委托完成

程序员文章站 2022-06-21 22:30:00
通过AsyncWatiHandle属性,访问等待句柄。WaitOne()方法阻断当前线程,直到异步调用线程完成返回可以利用的句柄以后再执行当前线程。 程序: [html]&nb...

通过AsyncWatiHandle属性,访问等待句柄。WaitOne()方法阻断当前线程,直到异步调用线程完成返回可以利用的句柄以后再执行当前线程。

程序:

[html] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
 
namespace AsyncDelegate 

    class Program 
    { 
        public delegate int TakeSomeTimeDelegate(int data,int ms); 
 
        static void Main(string[] args) 
        { 
            TakeSomeTimeDelegate dl=TakeSomeTime; 
            IAsyncResult ar=dl.BeginInvoke(1,200,null,null); 
 
            while (true) 
            { 
                Console.WriteLine("."); 
                Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId); 
                if (ar.AsyncWaitHandle.WaitOne(100, false)) 
                { 
                    Console.WriteLine("Can get the result now"); 
                    break; 
                } 
            } 
 
            //while (!ar.IsCompleted) 
            //{ 
            //    Console.WriteLine("."); 
            //    Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId); 
            //    Thread.Sleep(50); 
            //} 
 
            int result = dl.EndInvoke(ar); 
            Console.WriteLine("Result:{0}", result); 
        } 
 
        static int TakeSomeTime(int data, int ms) 
        { 
            Console.WriteLine("TakeSomeTime started!"); 
            Console.WriteLine("Run in thread:"+Thread.CurrentThread.ManagedThreadId); 
            Thread.Sleep(ms); 
            Console.WriteLine("TakeSomeTime Completed!"); 
            return ++data; 
        } 
    } 

[html] 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
 
namespace AsyncDelegate 

    class Program 
    { 
        public delegate int TakeSomeTimeDelegate(int data,int ms); 
 
        static void Main(string[] args) 
        { 
            TakeSomeTimeDelegate dl=TakeSomeTime; 
            IAsyncResult ar=dl.BeginInvoke(1,200,null,null); 
 
            while (true) 
            { 
                Console.WriteLine("."); 
                Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId); 
                if (ar.AsyncWaitHandle.WaitOne(100, false)) 
                { 
                    Console.WriteLine("Can get the result now"); 
                    break; 
                } 
            } 
 
            //while (!ar.IsCompleted) 
            //{ 
            //    Console.WriteLine("."); 
            //    Console.WriteLine("Run in thread:" + Thread.CurrentThread.ManagedThreadId); 
            //    Thread.Sleep(50); 
            //} 
 
            int result = dl.EndInvoke(ar); 
            Console.WriteLine("Result:{0}", result); 
        } 
 
        static int TakeSomeTime(int data, int ms) 
        { 
            Console.WriteLine("TakeSomeTime started!"); 
            Console.WriteLine("Run in thread:"+Thread.CurrentThread.ManagedThreadId); 
            Thread.Sleep(ms); 
            Console.WriteLine("TakeSomeTime Completed!"); 
            return ++data; 
        } 
    } 

运行结果:

 等待句柄判断异步委托完成

 

 

ar.AsyncWaitHandle.WaitOne()阻断了当前线程, 直到异步调用线程完成获得可以利用的句柄以后再次执行当前线程。

 作者:xuhongwei0411