国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#異步調用實例小結

2020-01-24 01:33:25
字體:
來源:轉載
供稿:網友

本文實例講述了C#異步調用的方法。分享給大家供大家參考。具體如下:

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>  /// 實現委托的方法  /// </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: 同步調用方法#region 示例 1: 同步調用方法  /// <summary>  /// 示例 1: 同步調用方法  /// </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() 調用模式異步調用方法  /// <summary>  /// 示例 2: 通過 EndInvoke() 調用模式異步調用方法    /// </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: 異步調用方法并使用 A WaitHandle 來等待調用完成  /// <summary>  /// 示例 3: 異步調用方法并使用 A WaitHandle 來等待調用完成  /// </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: 異步調用方法通過輪詢調用模式  /// <summary>  /// 示例 4: 異步調用方法通過輪詢調用模式  /// </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: 異步方法完成后執行回調  /// <summary>  /// 示例 5: 異步方法完成后執行回調  /// </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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鞍山市| 裕民县| 新龙县| 会宁县| 和政县| 成武县| 西青区| 齐齐哈尔市| 凤山市| 隆林| 乌鲁木齐市| 化州市| 宝兴县| 南涧| 将乐县| 稷山县| 德江县| 通化县| 安仁县| 东乡族自治县| 乐山市| 白河县| 磐石市| 佛山市| 古田县| 巨野县| 吕梁市| 锡林浩特市| 乐陵市| 启东市| 定襄县| 普格县| 伊金霍洛旗| 伊春市| 磐安县| 牟定县| 重庆市| 镇原县| 长武县| 安顺市| 奉贤区|