教程集 www.jiaochengji.com
教程集 >  脚本编程  >  Asp.net  >  正文 c# 异步调用的代码(简单示例)

c# 异步调用的代码(简单示例)

发布时间:2016-01-10   编辑:jiaochengji.com
c#实现异步调用的例子,是理解同步调用与异步调用区别与实现方法的好例子,有需要的朋友可以参考下。

c#实现异步调用的例子。

复制代码 代码示例:

///c3 异步调用
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() 调用模式异步调用方法
 /**/
 /*
  * 使用调用模式是要调用 BeginInvoke , 做某些处理主线程, 并调用 EndInvoke() 。
  * 注意不 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 来等待调用完成
 /**/
 /*
  * 由 BeginInvoke() 返回 IAsyncResult 具有一个 AsyncWaitHandle 属性。
  * 该属性返回 WaitHandle 异步调用完成后, 通知是。 等待 WaitHandle 是常见线程同步技术。
  * 通过是 WaitHandle WaitOne() 方法调用线程等待 WaitHandle 上。
  * 直到是通知 WaitHandle WaitOne() 块。 当 WaitOne() 返回, 您在调用 EndInvoke() 之前进行一些额外工作。
  * 对于执行文件或网络操作, 否则会阻塞调用主线程存为, 以前示例中此技术很有用。
  * */
 /**/
 /// <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: 异步调用方法通过轮询调用模式
 /**/
 /*
  * 由 BeginInvoke() 返回 IAsyncResult 对象有个 IsCompleted 属性异步调用完成后返回 True 。
  * 然后可调用 EndInvoke() 。 如果您应用程序不断工作对不做要长期函数调用已被此调用模式很有用。
  * MicrosoftWindows 应用程序是这样的示例。
  * 主线程的 Windows 应用程序可以继续以执行异步调用时处理用户输入。
  * 它可定期检查 IsCompleted 到调用是否完成。 它调用 EndInvoke 当 IsCompleted 返回 True 。
  * 直到它知道操作已完成因为 EndInvoke() 阻止直到异步操作为完整, 应用程序不调用它。
  * */
 /**/
 /// <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: 异步方法完成后执行回调
 /**/
 /*
  * 本节, 中示例提供对 BeginInvoke() 函数, 异步调用完成后系统执行回调委托。
  * 回调调用 EndInvoke() 并处理异步调用的结果。
  * 如果启动异步调用线程不需要处理结果是调用此调用模式很有用。
  * 异步调用完成后系统调用线程以外启动线程上调。
  * 若使用此调用模式, 作为第二到最后 - BeginInvoke() 函数的参数必须传递 AsyncCallback 类型的委托。
  * BeginInvoke() 还有最后参数键入 对象 到您可以将任何对象。 当它调用该对象可用于您回调函数。
  * 为此参数一个重要用途是以传递用于初始化调用该委托。
  * 回调函数然后使用与该委托 EndInvoke() 函数来完成调用。 此调用模式是所示。
  * by http://www.jbxue.com
  * */
 /**/
 /// <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();
 }
}
}

您可能感兴趣的文章:

[关闭]
~ ~