C#異步調(diào)用實例小結(jié)
本文實例講述了C#異步調(diào)用的方法。分享給大家供大家參考。具體如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Threading; using System.Windows.Forms; namespace CW { public partial class AsyncDemo : Form { public AsyncDemo() { InitializeComponent(); } private void Delgate_Load(object sender, EventArgs e) { } /// <summary> /// 實現(xiàn)委托的方法 /// </summary> /// <param name="iCallTime"></param> /// <param name="iExecThread"></param> /// <returns></returns> string LongRunningMethod(int iCallTime, out int iExecThread) { Thread.Sleep(iCallTime); iExecThread = AppDomain.GetCurrentThreadId(); return "MyCallTime was " + iCallTime.ToString(); } delegate string MethodDelegate(int iCallTime, out int iExecThread); #region 示例 1: 同步調(diào)用方法#region 示例 1: 同步調(diào)用方法 /// <summary> /// 示例 1: 同步調(diào)用方法 /// </summary> public void DemoSyncCall() { string s; int iExecThread; // Create an instance of a delegate that wraps LongRunningMethod. MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod); // Call LongRunningMethod using the delegate. s = dlgt(3000, out iExecThread); MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the thread ID {1}", s, iExecThread.ToString() ) ); } #endregion #region 示例 2: 通過 EndInvoke() 調(diào)用模式異步調(diào)用方法 /// <summary> /// 示例 2: 通過 EndInvoke() 調(diào)用模式異步調(diào)用方法 /// </summary> public void DemoEndInvoke() { MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod); string s; int iExecThread; // Initiate the asynchronous call. IAsyncResult ar = dlgt.BeginInvoke(5000, out iExecThread, null, null); // Do some useful work here. This would be work you want to have // run at the same time as the asynchronous call. // Retrieve the results of the asynchronous call. s = dlgt.EndInvoke(out iExecThread, ar); MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString() ) ); } #endregion #region 示例 3: 異步調(diào)用方法并使用 A WaitHandle 來等待調(diào)用完成 /// <summary> /// 示例 3: 異步調(diào)用方法并使用 A WaitHandle 來等待調(diào)用完成 /// </summary> public void DemoWaitHandle() { string s; int iExecThread; MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod); // Initiate the asynchronous call. IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null); // Do some useful work here. This would be work you want to have // run at the same time as the asynchronous call. // Wait for the WaitHandle to become signaled. ar.AsyncWaitHandle.WaitOne(); // Get the results of the asynchronous call. s = dlgt.EndInvoke(out iExecThread, ar); MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString() ) ); } #endregion #region 示例 4: 異步調(diào)用方法通過輪詢調(diào)用模式 /// <summary> /// 示例 4: 異步調(diào)用方法通過輪詢調(diào)用模式 /// </summary> public void DemoPolling() { MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod); string s; int iExecThread; // Initiate the asynchronous call. IAsyncResult ar = dlgt.BeginInvoke(3000, out iExecThread, null, null); // Poll IAsyncResult.IsCompleted while (ar.IsCompleted == false) { Thread.Sleep(10); // pretend to so some useful work } s = dlgt.EndInvoke(out iExecThread, ar); MessageBox.Show(string.Format ("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString() ) ); } #endregion #region 示例 5: 異步方法完成后執(zhí)行回調(diào) /// <summary> /// 示例 5: 異步方法完成后執(zhí)行回調(diào) /// </summary> public void DemoCallback() { MethodDelegate dlgt = new MethodDelegate(this.LongRunningMethod); int iExecThread; // Create the callback delegate. AsyncCallback cb = new AsyncCallback(MyAsyncCallback); // Initiate the Asynchronous call passing in the callback delegate // and the delegate object used to initiate the call. IAsyncResult ar = dlgt.BeginInvoke(5000, out iExecThread, cb, dlgt); } public void MyAsyncCallback(IAsyncResult ar) { string s; int iExecThread; // Because you passed your original delegate in the asyncState parameter // of the Begin call, you can get it back here to complete the call. MethodDelegate dlgt = (MethodDelegate)ar.AsyncState; // Complete the call. s = dlgt.EndInvoke(out iExecThread, ar); MessageBox.Show(String.Format("The delegate call returned the string: {0}, and the number {1}", s, iExecThread.ToString())); //Console.WriteLine(string.Format ("The delegate call returned the string: "{0}", and the number {1}", s, iExecThread.ToString() ) ); } #endregion private void button1_Click(object sender, EventArgs e) { //DemoSyncCall() ; //DemoEndInvoke(); //DemoWaitHandle(); //DemoPolling(); DemoCallback(); } } }
希望本文所述對大家的C#程序設(shè)計有所幫助。
相關(guān)文章
C# TextBox多行文本框的字?jǐn)?shù)限制問題
最近在使用C# TextBox多行文本框的時候,發(fā)現(xiàn)了其對字?jǐn)?shù)限制的一點問題,所以總結(jié)下在使用C# TextBox多行文本框要注意的的字?jǐn)?shù)限制問題,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12C# 6.0 內(nèi)插字符串(Interpolated Strings )的使用方法
這篇文章主要為大家詳細(xì)介紹了C# 6.0 內(nèi)插字符串的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11WindowsForm移動一個沒有標(biāo)題欄的窗口的方法
這篇文章主要介紹了WindowsForm移動一個沒有標(biāo)題欄的窗口的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07c#橋接模式(bridge結(jié)構(gòu)模式)用法實例
這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細(xì)的分析了橋接模式的原理與用法實例,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12c# 動態(tài)加載dll文件,并實現(xiàn)調(diào)用其中的方法(推薦)
下面小編就為大家?guī)硪黄猚# 動態(tài)加載dll文件,并實現(xiàn)調(diào)用其中的方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02用c#獲得當(dāng)前用戶的Application Data文件夾位置
用c#獲得當(dāng)前用戶的Application Data文件夾位置...2007-03-03C#?PictureBox控件方法參數(shù)及圖片刪除重命名上傳詳解
這篇文章主要為大家介紹了C#?PictureBox控件方法參數(shù)及圖片刪除重命名上傳示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08深入C#任務(wù)管理器中應(yīng)用程序選項隱藏程序本身的方法詳解
本篇文章是對在C#任務(wù)管理器中應(yīng)用程序選項隱藏程序本身的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05