• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > C#异步调用实例小结

C#异步调用实例小结

作者:软件工程师 字体:[增加 减小] 来源:互联网 时间:2017-05-28

软件工程师 通过本文主要向大家介绍了马桶c的个人空间,c语言,欲情 c max,维生素c,奔驰c200等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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();
  }
 }
}

</div>

希望本文所述对大家的C#程序设计有所帮助。

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • C#利用ReportViewer生成报表
  • C#基于正则去掉注释的方法示例
  • C#中new的用法及与override的区别分析
  • C#实现两个richtextbox控件滚动条同步滚动的简单方法
  • C# for循环的经典案例集锦
  • C#操作word的方法示例
  • C#使用WebClient登录网站并抓取登录后的网页信息实现方法
  • C# WinForm制作异形窗体与控件的方法
  • C#实现Excel表数据导入Sql Server数据库中的方法
  • C#使用NPOI上传excel

相关文章

  • 2017-05-28C#的泛型方法解析
  • 2017-05-28c#实现万年历示例分享 万年历农历查询
  • 2017-05-28C#获得MAC地址(网卡序列号)的实现代码
  • 2017-05-28winform去掉右上角关闭按钮的方法
  • 2017-05-28使用Npoi操作excel的解决办法
  • 2017-05-28C#实现带消息数的App图标
  • 2017-05-28深入分析C#键盘勾子(Hook)拦截器,屏蔽键盘活动的详解
  • 2017-05-28C#跨窗体操作(引用传递) 实例代码
  • 2017-05-28c#接口使用示例分享
  • 2017-05-28C#列出所有物理网络适配器的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#中进程的挂起与恢复
    • 高效C#编码优化原则
    • C#图像处理之头发检测的方法
    • C#编程和Visual Studio使用技巧(上)
    • C#文件合并的方法
    • C#通过WIN32 API实现嵌入程序窗体
    • 举例讲解C#中自动实现的属性
    • C#文件下载实例代码(适用于各个浏览器)
    • c# List find()方法返回值的问题说明(返回结果为对象的指针)
    • C# Redis学习系列(二)Redis基本设置

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有