C#异步调用实例小结
作者:软件工程师 发布时间:2023-07-16 10:31:03
标签: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#程序设计有所帮助。


猜你喜欢
- 创建一个简单的项目:<?xml version="1.0" encoding="UTF-8"?
- 开始接触分布式概念,学习之前要准备搭建Dubbo和Zookeeper环境的简单搭建。Window下安装Zookeeper和Dubbo-adm
- 一、MyBatis简介MyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis消除了几乎所有的JDBC代码和参
- 本文实例为大家分享了Android自定义view贝塞尔曲线,供大家参考,具体内容如下贝塞尔曲线以一个简单的贝塞尔曲线为例,二阶曲线原理贝塞尔
- 谷歌官方提供了apktool可以逆向已经发布出去的APK应用,即反编译已经打包成功的APK文件,使用它可以将其反编译成非常接近打包前的原始格
- 目录定义数据库表访问表中的数据插入数据查询数据创建数据库测试 DaoRoom 是 SQLite 的封装,它使 Android 对数据库的操作
- Java的NIO中的管道,就类似于实际中的管道,有两端,一段作为输入,一段作为输出。也就是说,在创建了一个管道后,既可以对管道进行写,也可以
- 1、新建一个Activity,并把各个生命周期打印出来 2、运行Activity,得到如下信息 onCreate--> onStart
- 在一些环境中,可能需要把Web应用做成无状态的,即服务器端无状态,就是说服务器端不会存储像会话这种东西,而是每次请求时带上相应的用户名进行登
- 在spring中有很多以XXXAware命名的接口,很多人也不清楚这些接口都是做什么用的,这篇文章将描述常用的一些接口。一,Applicat
- Java执行hadoop的基本操作实例代码向HDFS上传本地文件public static void uploadInputFile(Str
- 简介本文主要讲解java 利用Selenium 操作浏览器网站时候,需要用的js的地方,代码该如何实现。调用JavaScriptwebdri
- 本文实例讲述了C#使用foreach遍历哈希表(hashtable)的方法。分享给大家供大家参考。具体实现方法如下:using System
- 此次简单的操作将数据从数据库导出生成excel报表以及将excel数据导入数据库首先建立数据库的连接池:package jdbc;impor
- 1、spring-cloud-starter-alibaba-nacos-discovery 这里依赖报红,无法引入,或显示无法找到,更换版
- 一、项目整体介绍:项目整体的结构如下图所示,项目整体采用 springboot + mybatis + jsp + mysql 来完成的,下
- 加密解密本身并不是难事,问题是在何时去处理?定义一个过滤器,将请求和响应分别拦截下来进行处理也是一个办法,这种方式虽然粗暴,但是灵活,因为可
- 本文实例讲述了Android基于DialogFragment创建对话框的方法。分享给大家供大家参考,具体如下:/** * 使用DialogF
- 本文介绍了ListView给每个Item上面的按钮添加事件,具体如下:1.先看下效果图:在这里仅供测试,我把数据都写死了,根据需要可以自己进
- 做Android开发的童靴们肯定对系统自带的控件使用的都非常熟悉,比如Button、TextView、ImageView等。如果你问我具体使