首页前端开发HTML等待句柄判断异步委托完成

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

时间2024-01-25 13:34:23发布访客分类HTML浏览872
导读:收集整理的这篇文章主要介绍了html5教程-等待句柄判断异步委托完成,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。 通过AsyncWatiHand...
收集整理的这篇文章主要介绍了html5教程-等待句柄判断异步委托完成,觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

通过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

通过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

觉得可用,就经常来吧! 欢迎评论哦! html5教程,巧夺天工,精雕玉琢。小宝典献丑了!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

ClassdivHTMLletpost-format-gallery

若转载请注明出处: 等待句柄判断异步委托完成
本文地址: https://pptw.com/jishu/586580.html
html5 canvas的clearRect HTML5中的拼写检查

游客 回复需填写必要信息