异步

    技术2024-11-03  30

    public delegate string aysCompute(string k);namespace ConsoleApplication1{    class Class4    {

            static void Main(string[] args)        {            Compute cP = new Compute();            asynCompute aC = new asynCompute(cP.comp);            Console.WriteLine("Begin asyn call.");            IAsyncResult result = aC.BeginInvoke(5, null, null);            Console.WriteLine("Wait for end...");            //WaitOne阻塞主线程,如果此线程负责UI,则不应在主线程里这么用。            result.AsyncWaitHandle.WaitOne();            //用while循环轮询result的状态,这与WaitOne稍有不同,不过也会阻塞主线程,不好            while (result.IsCompleted == false)            {                Thread.Sleep(10);                Console.WriteLine("waiting...");            }            //取得计算返回值            int value = aC.EndInvoke(result);            Console.WriteLine("value:" + value.ToString());            Console.ReadLine();        }

            static void Main(string[] args)        {            Compute cP = new Compute();            asynCompute aC = new asynCompute(cP.comp);            Console.WriteLine("Begin asyn call.");            IAsyncResult result = aC.BeginInvoke(5, new AsyncCallback(asynEndCall), aC);            Console.WriteLine("Wait for end...");            Console.WriteLine("主线程做了些事……");            Console.ReadLine();        }

     

            static void asynEndCall(IAsyncResult ar)        {            Console.WriteLine("异步回调开始……");            asynCompute aC = (asynCompute)ar.AsyncState;            int value = aC.EndInvoke(ar);            Console.WriteLine("异步回调结束……");            Console.WriteLine("result:" + value.ToString());        }

            public class Compute        {            public Compute()            { }

                public string comp(string k)            {                Console.WriteLine("In Compute.comp");                Thread.Sleep(100);                return k;            }        }    }}

     public class GERIAnsync    {        public GERIAnsync()        {

            }

            public delegate int UpdateOfficerLocationDelegate(decimal lat, decimal lon, string officeName);        public delegate int UpdateFloodTodayDelegate(string strVal);        public delegate int DeleteFloodTodayDelegate(List<string> idList);        public delegate int FlushDelegate();        public delegate int UpdateFloodHistoryDelegate(ReportDetailDataSet reportDetailDS);

            public void UpdateOfficerLocationAnsync(decimal lat, decimal lon, string officeName)        {            UpdateOfficerLocation objStaffPosition = new UpdateOfficerLocation();            UpdateOfficerLocationDelegate objDel = new UpdateOfficerLocationDelegate(objStaffPosition.UpdateOfficerLocationMethod);            IAsyncResult AsyncRes = objDel.BeginInvoke(lat, lon, officeName, new AsyncCallback(UpdateOfficerLocationCallBack), "UpdateOfficerLocationAnsync");            PFAS.Common.PUBUtility.AppLog(PUBConstant.LogType.INFO, "Invoke UpdateOfficerLocationAnsync");        }

            public static void UpdateOfficerLocationCallBack(IAsyncResult ar)        {            AsyncResult Result = (AsyncResult)ar;            UpdateOfficerLocationDelegate objDel = (UpdateOfficerLocationDelegate)Result.AsyncDelegate;            int intResult = objDel.EndInvoke(ar);            //Console.WriteLine("Output from callback: " + intResult);            //state information from the main thread            //string strMessage = (string)ar.AsyncState;            //Console.WriteLine(strMessage);

            }

            public void UpdateFloodTodayAnsync(string strVal)        {            UpdateFloodToday objUpdateFloodToday = new UpdateFloodToday();            UpdateFloodTodayDelegate objDel = new UpdateFloodTodayDelegate(objUpdateFloodToday.UpdateFloodTodayMethod);            IAsyncResult AsyncRes = objDel.BeginInvoke(strVal, new AsyncCallback(UpdateFloodTodayCallBack), "UpdateFloodTodayAnsync");            PFAS.Common.PUBUtility.AppLog(PUBConstant.LogType.INFO, "Invoke UpdateFloodTodayAnsync");        }

            public static void UpdateFloodTodayCallBack(IAsyncResult ar)        {            AsyncResult Result = (AsyncResult)ar;            UpdateFloodTodayDelegate objDel = (UpdateFloodTodayDelegate)Result.AsyncDelegate;            int intResult = objDel.EndInvoke(ar);        }

            public void DeleteFloodTodayAnsync(List<string> idList)        {            DeleteFloodToday objDeleteFloodToday = new DeleteFloodToday();            DeleteFloodTodayDelegate objDel = new DeleteFloodTodayDelegate(objDeleteFloodToday.DeleteFloodTodayMethod);            IAsyncResult AsyncRes = objDel.BeginInvoke(idList, new AsyncCallback(DeleteFloodTodayCallBack), "DeleteFloodTodayAnsync");            PFAS.Common.PUBUtility.AppLog(PUBConstant.LogType.INFO, "Invoke DeleteFloodTodayAnsync");        }

            public static void DeleteFloodTodayCallBack(IAsyncResult ar)        {            AsyncResult Result = (AsyncResult)ar;            DeleteFloodTodayDelegate objDel = (DeleteFloodTodayDelegate)Result.AsyncDelegate;            int intResult = objDel.EndInvoke(ar);        }

            public void FlushAnsync()        {            Flush objFlush = new Flush();            FlushDelegate objDel = new FlushDelegate(objFlush.FlushMethod);            IAsyncResult AsyncRes = objDel.BeginInvoke(new AsyncCallback(FlushCallBack), "FlushAnsync");            PFAS.Common.PUBUtility.AppLog(PUBConstant.LogType.INFO, "Invoke FlushAnsync");        }

            public static void FlushCallBack(IAsyncResult ar)        {            AsyncResult Result = (AsyncResult)ar;            FlushDelegate objDel = (FlushDelegate)Result.AsyncDelegate;            int intResult = objDel.EndInvoke(ar);        }

            public void UpdateFloodHistoryAnsync(ReportDetailDataSet reportDetailDS)        {            UpdateFloodHistory objUpdateFloodHistory = new UpdateFloodHistory();            UpdateFloodHistoryDelegate objDel = new UpdateFloodHistoryDelegate(objUpdateFloodHistory.UpdateFloodHistoryMethod);            IAsyncResult AsyncRes = objDel.BeginInvoke(reportDetailDS, new AsyncCallback(UpdateFloodHistoryCallBack), "UpdateFloodHistoryAnsync");            PFAS.Common.PUBUtility.AppLog(PUBConstant.LogType.INFO, "Invoke UpdateFloodHistoryAnsync");        }

            public static void UpdateFloodHistoryCallBack(IAsyncResult ar)        {            AsyncResult Result = (AsyncResult)ar;            UpdateFloodHistoryDelegate objDel = (UpdateFloodHistoryDelegate)Result.AsyncDelegate;            int intResult = objDel.EndInvoke(ar);        }    }

     

     

     

    最新回复(0)