C#异步调用实例小结
本文实例讲述了C#异步调用的方法。分享给大家供大家参考。具体如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Threading; usingSystem.Windows.Forms; namespaceCW { publicpartialclassAsyncDemo:Form { publicAsyncDemo() { InitializeComponent(); } privatevoidDelgate_Load(objectsender,EventArgse) { } ///<summary> ///实现委托的方法 ///</summary> ///<paramname="iCallTime"></param> ///<paramname="iExecThread"></param> ///<returns></returns> stringLongRunningMethod(intiCallTime,outintiExecThread) { Thread.Sleep(iCallTime); iExecThread=AppDomain.GetCurrentThreadId(); return"MyCallTimewas"+iCallTime.ToString(); } delegatestringMethodDelegate(intiCallTime,outintiExecThread); #region示例1:同步调用方法#region示例1:同步调用方法 ///<summary> ///示例1:同步调用方法 ///</summary> publicvoidDemoSyncCall() { strings; intiExecThread; //CreateaninstanceofadelegatethatwrapsLongRunningMethod. MethodDelegatedlgt=newMethodDelegate(this.LongRunningMethod); //CallLongRunningMethodusingthedelegate. s=dlgt(3000,outiExecThread); MessageBox.Show(string.Format("Thedelegatecallreturnedthestring:{0},andthethreadID{1}",s,iExecThread.ToString())); } #endregion #region示例2:通过EndInvoke()调用模式异步调用方法 ///<summary> ///示例2:通过EndInvoke()调用模式异步调用方法 ///</summary> publicvoidDemoEndInvoke() { MethodDelegatedlgt=newMethodDelegate(this.LongRunningMethod); strings; intiExecThread; //Initiatetheasynchronouscall. IAsyncResultar=dlgt.BeginInvoke(5000,outiExecThread,null,null); //Dosomeusefulworkhere.Thiswouldbeworkyouwanttohave //runatthesametimeastheasynchronouscall. //Retrievetheresultsoftheasynchronouscall. s=dlgt.EndInvoke(outiExecThread,ar); MessageBox.Show(string.Format("Thedelegatecallreturnedthestring:{0},andthenumber{1}",s,iExecThread.ToString())); } #endregion #region示例3:异步调用方法并使用AWaitHandle来等待调用完成 ///<summary> ///示例3:异步调用方法并使用AWaitHandle来等待调用完成 ///</summary> publicvoidDemoWaitHandle() { strings; intiExecThread; MethodDelegatedlgt=newMethodDelegate(this.LongRunningMethod); //Initiatetheasynchronouscall. IAsyncResultar=dlgt.BeginInvoke(3000,outiExecThread,null,null); //Dosomeusefulworkhere.Thiswouldbeworkyouwanttohave //runatthesametimeastheasynchronouscall. //WaitfortheWaitHandletobecomesignaled. ar.AsyncWaitHandle.WaitOne(); //Gettheresultsoftheasynchronouscall. s=dlgt.EndInvoke(outiExecThread,ar); MessageBox.Show(string.Format("Thedelegatecallreturnedthestring:{0},andthenumber{1}",s,iExecThread.ToString())); } #endregion #region示例4:异步调用方法通过轮询调用模式 ///<summary> ///示例4:异步调用方法通过轮询调用模式 ///</summary> publicvoidDemoPolling() { MethodDelegatedlgt=newMethodDelegate(this.LongRunningMethod); strings; intiExecThread; //Initiatetheasynchronouscall. IAsyncResultar=dlgt.BeginInvoke(3000,outiExecThread,null,null); //PollIAsyncResult.IsCompleted while(ar.IsCompleted==false) { Thread.Sleep(10);//pretendtososomeusefulwork } s=dlgt.EndInvoke(outiExecThread,ar); MessageBox.Show(string.Format("Thedelegatecallreturnedthestring:{0},andthenumber{1}",s,iExecThread.ToString())); } #endregion #region示例5:异步方法完成后执行回调 ///<summary> ///示例5:异步方法完成后执行回调 ///</summary> publicvoidDemoCallback() { MethodDelegatedlgt=newMethodDelegate(this.LongRunningMethod); intiExecThread; //Createthecallbackdelegate. AsyncCallbackcb=newAsyncCallback(MyAsyncCallback); //InitiatetheAsynchronouscallpassinginthecallbackdelegate //andthedelegateobjectusedtoinitiatethecall. IAsyncResultar=dlgt.BeginInvoke(5000,outiExecThread,cb,dlgt); } publicvoidMyAsyncCallback(IAsyncResultar) { strings; intiExecThread; //BecauseyoupassedyouroriginaldelegateintheasyncStateparameter //oftheBegincall,youcangetitbackheretocompletethecall. MethodDelegatedlgt=(MethodDelegate)ar.AsyncState; //Completethecall. s=dlgt.EndInvoke(outiExecThread,ar); MessageBox.Show(String.Format("Thedelegatecallreturnedthestring:{0},andthenumber{1}",s,iExecThread.ToString())); //Console.WriteLine(string.Format("Thedelegatecallreturnedthestring:"{0}",andthenumber{1}",s,iExecThread.ToString())); } #endregion privatevoidbutton1_Click(objectsender,EventArgse) { //DemoSyncCall(); //DemoEndInvoke(); //DemoWaitHandle(); //DemoPolling(); DemoCallback(); } } }
希望本文所述对大家的C#程序设计有所帮助。