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#程序设计有所帮助。
0
投稿
猜你喜欢
- 1. 字段取别名,和属性名保持一致映射文件<mapper namespace="com.atguigu.mybatis.ma
- 本文实例为大家分享了PropertyDescriptor反射调用set和get方法,供大家参考,具体内容如下第一段:package com.
- 对Jpa Entity关系映射中mappedBy的理解mappedBy 单向关系不需要设置该属性,双向关系必须设置,避免双方都建立外键字段数
- Java 8支持动态语言,看到了很酷的Lambda表达式,对一直以静态类型语言自居的Java,让人看到了Java虚拟机可以支持动态语言的目标
- 1.特殊数组的特征值题目描述思路详解看到本题,首先思考需要排序,然后查找,这里为了效率采用二分查找。假设定义x=(left+riht)/ 2
- Stream简化元素计算一、接口设计从Java1.8开始提出了Stream流的概念,侧重对于源数据计算能力的封装,并且支持序列与并行两种操作
- 本文为大家分享了Android实现带动画效果的可点击展开TextView 制作代码,效果图: 收起(默认)效果:点击展开后的效果:源码: 布
- 我们知道,Maven 是通过仓库对依赖进行管理的,当 Maven 项目需要某个依赖时,只要其 POM 中声明了依赖的坐标信息,Maven 就
- 一、前言正常情况下classloader只能找到jar里面当前目录或者文件类里面的*.class文件。为了能够加载嵌套jar里面的资源之前都
- 1、引言你能搜到这个教程,说明你对 Maven 感兴趣,但是又不是太理解。那么接下来这个系列的教程将会详细讲解 Maven 的用法,相信你看
- 本文实例为大家分享了unity shader实现光照效果的具体代码,供大家参考,具体内容如下效果图:shader被附给了球。灯光需要在属性面
- 前提:windows上安装jdk1.启动jar脚本@echo offSTART "app" javaw -jar app
- 1.alibaba falstjson1.Map转JSONMap<String, Object> map = new HashM
- java 8引入了lambda表达式,lambda表达式实际上表示的就是一个匿名的function。在java 8之前,如果需要使用到匿名f
- Java事件处理机制java中的事件机制的参与者有3种角色:1.event object:事件状态对象,用于listener的相应的方法之中
- 前言任何一个服务如果没有监控,那就是两眼一抹黑,无法知道当前服务的运行情况,也就无法对可能出现的异常状况进行很好的处理,所以对任意一个服务来
- 星期天小哼和小哈约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏——“小猫钓鱼”。游戏的规则是这样的:将一副扑克牌平均分成两份,每人拿一份。
- Maven Release当我们的项目达到了当前的目标,在经过检测后不需要改变。这时我们就需要将SNAPSHOT版本打包成RELEASE版本
- 前言本身我是一个比较偏向少使用Stream的人,因为调试比较不方便。但是, 不得不说,stream确实会给我们编码带来便捷。Stream流
- 在javaweb中写了一个图片的链接,可以打开预览,另外提供一个下载功能。以下是预览代码,没什么好说的;href若连接的是一个压缩包文件之类