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>
  /// 实现委托的方法
  /// </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#程序设计有所帮助。

相关文章

  • C#中当前时间转为时间戳的3个方法

    C#中当前时间转为时间戳的3个方法

    在计算机应用世界里,无论是为了记录事件的发生时间、保障数据一致性还是提升安全性,时间戳都是不可或缺的重要工具,下面我们就来看看C#中转换当前时间为时间戳有哪些方法吧
    2024-12-12
  • C#中WebBroeser控件用法实例教程

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

    这篇文章主要介绍了C#中WebBroeser控件用法,包括了常用属性、事件处理及应用实例,需要的朋友可以参考下
    2014-09-09
  • C# as 和 is 运算符区别和用法示例解析

    C# as 和 is 运算符区别和用法示例解析

    在C#中,as 和 is 关键字都用于处理类型转换的运算符,但它们有不同的用途和行为,本文我们将详细解释这两个运算符的区别和用法,需要的朋友可以参考下
    2025-01-01
  • C#实现Winform版计算器

    C#实现Winform版计算器

    这篇文章主要为大家详细介绍了C#实现Winform版计算器,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-05-05
  • c# WinForm 窗体之间传值的几种方式(小结)

    c# WinForm 窗体之间传值的几种方式(小结)

    这篇文章主要介绍了WinForm 窗体之间传值的几种方式(小结),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • C#中的TemplateMethod模式问题分析

    C#中的TemplateMethod模式问题分析

    这篇文章主要介绍了C#中的TemplateMethod模式,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • C#实现改变DataGrid某一行和单元格颜色的方法

    C#实现改变DataGrid某一行和单元格颜色的方法

    这篇文章主要介绍了C#实现改变DataGrid某一行和单元格颜色的方法,主要涉及DataGrid控件的添加与使用、数据源的绑定、单元格与行的获取等操作。需要的朋友可以参考下
    2014-09-09
  • c#生成excel示例sql数据库导出excel

    c#生成excel示例sql数据库导出excel

    这篇文章主要介绍了c#操作excel的示例,里面的方法可以直接导出数据到excel,大家参考使用吧
    2014-01-01
  • C#实现ArrayList动态数组的示例

    C#实现ArrayList动态数组的示例

    ArrayList是一个动态数组,可以用来存储任意类型的元素,本文就来介绍一下C#实现ArrayList动态数组的示例,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12
  • 总结C#网络编程中对于Cookie的设定要点

    总结C#网络编程中对于Cookie的设定要点

    这篇文章主要介绍了总结C#网络编程中对于Cookie的设定要点,文中还给出了一个cookie操作实例仅供参照,需要的朋友可以参考下
    2016-04-04

最新评论