最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#異步調用實例小結

 更新時間:2015年08月10日 17:51:58   作者:軟件工程師  
這篇文章主要介紹了C#異步調用的方法,實例分析了C#同步調用及異步調用的常用技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了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>
  /// 實現(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: 同步調用方法#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: 異步方法完成后執(zhí)行回調
  /// <summary>
  /// 示例 5: 異步方法完成后執(zhí)行回調
  /// </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#程序設計有所幫助。

相關文章

  • C#中當前時間轉為時間戳的3個方法

    C#中當前時間轉為時間戳的3個方法

    在計算機應用世界里,無論是為了記錄事件的發(fā)生時間、保障數(shù)據(jù)一致性還是提升安全性,時間戳都是不可或缺的重要工具,下面我們就來看看C#中轉換當前時間為時間戳有哪些方法吧
    2024-12-12
  • C#中WebBroeser控件用法實例教程

    C#中WebBroeser控件用法實例教程

    這篇文章主要介紹了C#中WebBroeser控件用法,包括了常用屬性、事件處理及應用實例,需要的朋友可以參考下
    2014-09-09
  • C#?as?和?is?運算符區(qū)別和用法示例解析

    C#?as?和?is?運算符區(qū)別和用法示例解析

    在C#中,as?和?is?關鍵字都用于處理類型轉換的運算符,但它們有不同的用途和行為,本文我們將詳細解釋這兩個運算符的區(qū)別和用法,需要的朋友可以參考下
    2025-01-01
  • C#實現(xiàn)Winform版計算器

    C#實現(xiàn)Winform版計算器

    這篇文章主要為大家詳細介紹了C#實現(xiàn)Winform版計算器,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • c# WinForm 窗體之間傳值的幾種方式(小結)

    c# WinForm 窗體之間傳值的幾種方式(小結)

    這篇文章主要介紹了WinForm 窗體之間傳值的幾種方式(小結),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09
  • C#中的TemplateMethod模式問題分析

    C#中的TemplateMethod模式問題分析

    這篇文章主要介紹了C#中的TemplateMethod模式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • C#實現(xiàn)改變DataGrid某一行和單元格顏色的方法

    C#實現(xiàn)改變DataGrid某一行和單元格顏色的方法

    這篇文章主要介紹了C#實現(xiàn)改變DataGrid某一行和單元格顏色的方法,主要涉及DataGrid控件的添加與使用、數(shù)據(jù)源的綁定、單元格與行的獲取等操作。需要的朋友可以參考下
    2014-09-09
  • c#生成excel示例sql數(shù)據(jù)庫導出excel

    c#生成excel示例sql數(shù)據(jù)庫導出excel

    這篇文章主要介紹了c#操作excel的示例,里面的方法可以直接導出數(shù)據(jù)到excel,大家參考使用吧
    2014-01-01
  • C#實現(xiàn)ArrayList動態(tài)數(shù)組的示例

    C#實現(xiàn)ArrayList動態(tài)數(shù)組的示例

    ArrayList是一個動態(tài)數(shù)組,可以用來存儲任意類型的元素,本文就來介紹一下C#實現(xiàn)ArrayList動態(tài)數(shù)組的示例,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • 總結C#網(wǎng)絡編程中對于Cookie的設定要點

    總結C#網(wǎng)絡編程中對于Cookie的設定要點

    這篇文章主要介紹了總結C#網(wǎng)絡編程中對于Cookie的設定要點,文中還給出了一個cookie操作實例僅供參照,需要的朋友可以參考下
    2016-04-04

最新評論

宁河县| 苏尼特右旗| 布尔津县| 苗栗市| 柳江县| 门源| 即墨市| 武平县| 玉门市| 新安县| 凯里市| 兰西县| 淄博市| 中方县| 双桥区| 沙坪坝区| 陈巴尔虎旗| 晋州市| 汽车| 连城县| 黄梅县| 咸丰县| 南宁市| 大新县| 丰顺县| 壤塘县| 申扎县| 韶关市| 海丰县| 东台市| 黔东| 阜新| 迭部县| 义马市| 延吉市| 萍乡市| 拜城县| 武穴市| 额尔古纳市| 雷州市| 长阳|