溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#異步回調(diào)函數(shù)

發(fā)布時(shí)間:2020-07-23 20:15:14 來(lái)源:網(wǎng)絡(luò) 閱讀:1704 作者:Aonaufly 欄目:編程語(yǔ)言


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ComprehensiveTest.com
{
    public class AsyCallEx112
    {
        // 定義一個(gè)執(zhí)行加法的委托
        public delegate int sum(int a, int b);
        public class number
        {
            public int m = 4;
            public int numberAdd(int a, int b)
            {
                int c = a + b;
                return c;
            }
            //定義一個(gè)與。net framework 定義的asyncCallback委托相對(duì)應(yīng)的回調(diào)函數(shù)
            public void CallbackMothed2(IAsyncResult ar2)
            {

                //numberadd.BeginInvoke(21, 12, numberBack, numberadd);              //第三個(gè)參數(shù)為回調(diào)  

                sum s = (sum)ar2.AsyncState;//獲得BeginInvoke第4個(gè)參數(shù)
                int number = s.EndInvoke(ar2);//獲取運(yùn)算的結(jié)果
                m = number;
                Console.WriteLine("得到的M值:{0}", m);
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace ComprehensiveTest.com
{
    public class AsyCallEx113
    {
        //定義一個(gè)委托
        public delegate void AsyncEventHanlder();
        public class Class1
        {
            public void Event1()
            {
                Console.WriteLine("Event1 start");
                System.Threading.Thread.Sleep(2000);
                Console.WriteLine("Event1 end");
            }
            public void Event2()
            {
                Console.WriteLine("Event2 start");
                int i = 1;
                while (i < 100)
                {
                    i = i + 1;
                    Console.WriteLine("Event2 " + i.ToString());
                }
                Console.WriteLine("Event2 end");
            }
            public void CallbackMethod( IAsyncResult ar )
            {
                ((AsyncEventHanlder)ar.AsyncState).EndInvoke(ar);
            }
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComprehensiveTest.com;


namespace ComprehensiveTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // 112
            //AsyCallEx112.number num = new AsyCallEx112.number();
            //AsyCallEx112.sum numberadd= new AsyCallEx112.sum(num.numberAdd);
            //AsyncCallback numberBack = new AsyncCallback(num.CallbackMothed2);
            //numberadd.BeginInvoke(21, 12, numberBack, numberadd);
            //Console.WriteLine("得到的結(jié)果:");
            //Console.WriteLine(num.m);


            // 113
            long start = 0;
            long end = 0;
            AsyCallEx113.Class1 c = new AsyCallEx113.Class1();
            Console.WriteLine("ready");
            start = DateTime.Now.Ticks;


            AsyCallEx113.AsyncEventHanlder asy = new AsyCallEx113.AsyncEventHanlder(c.Event1);
            //IAsyncResult ia = asy.BeginInvoke(null, null);


            IAsyncResult ia  = asy.BeginInvoke(new AsyncCallback(c.CallbackMethod), asy);
            ia.AsyncWaitHandle.WaitOne();     //對(duì)c.Event1產(chǎn)生阻塞,既是等Event1全部執(zhí)行完畢以后再往后面執(zhí)行代碼(!important)
            c.Event2();
           
            end = DateTime.Now.Ticks;
            Console.WriteLine("時(shí)間刻度差= " + Convert.ToString(end - start));
            Console.ReadLine();
        }
    }
}


不知道是什么原因 , 有的時(shí)候 , 沒(méi)法打中文 , 在這個(gè)編輯器里面 . 不過(guò)現(xiàn)在又好了,,,C#異步回調(diào)函數(shù)

廢話少說(shuō) , 進(jìn)入正題了

1 , 這個(gè)異步調(diào)用( CallBack )  于 傳統(tǒng)的callback的區(qū)別 , 就是1個(gè)在于異步 , 一個(gè)在于同步.當(dāng)然C#異步調(diào)用有方法是其變?yōu)橥秸{(diào)用 . 如果讀者現(xiàn)在不明白何為同步 , 何為異步 , 請(qǐng)找度娘 ......

過(guò)程如下 :

        ①:BeginInvoke方法啟動(dòng)異步調(diào)用 , 委托實(shí)例的方法 , 參數(shù)( n個(gè)回調(diào)參數(shù) , 實(shí)例AsyncCallback , 委托實(shí)例本身 ) . 其中 : 關(guān)于AsyncCallback 的參數(shù)是有IAsyncResult的Function  

        ②:EndInvoke檢索異步調(diào)用的結(jié)果 , 就是得到委托運(yùn)算的結(jié)果


使用WaitHandle等待異步調(diào)用

 就是產(chǎn)生與傳統(tǒng)Callback一樣的效果 , 即產(chǎn)生阻塞 , 等待異步調(diào)用全部完成 , 進(jìn)行下面的運(yùn)行

    由BeginInvoke 產(chǎn)生的IAsyncResult     ( IAsynResult.AsyncWaitHandle.WaitOne() )


請(qǐng) , 參照上面的代碼 , 學(xué)習(xí)!!! 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI