C#异步调用实例小结
作者:软件工程师 时间:2023-07-16 10:31:03
本文实例讲述了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#,异步,调用
0
投稿
猜你喜欢
Android 开发音频组件(Vitamio FAQ)详细介绍
2021-06-24 01:09:15
Java 中的抽象介绍
2023-03-27 06:10:20
C#图像处理之边缘检测(Sobel)的方法
2022-05-12 02:05:50
Android中使用Notification实现状态栏的通知
2022-09-12 00:49:39
Python实现JavaBeans流程详解
2021-05-29 05:37:39
微信公众平台(测试接口)准备工作
2023-05-24 15:51:06
Java 中的 String对象为什么是不可变的
2023-08-04 03:24:32
java加密算法分享(rsa解密、对称加密、md5加密)
2021-08-30 16:22:08
Android带进度条的文件上传示例(使用AsyncTask异步任务)
2023-06-24 09:43:11
android中图片的三级缓存cache策略(内存/文件/网络)
2023-09-04 19:51:59
Idea配置Maven阿里云镜像加速的实现
2021-10-24 11:30:32
ThreadLocal数据存储结构原理解析
2023-04-27 19:54:55
C#判断获取的是文件夹还是文件的实例
2022-11-25 05:00:01
Mybatis批量插入index out of range错误的解决(较偏的错误)
2022-06-11 01:11:51
Logback配置文件这么写,还说你不会整理日志?
2022-10-30 10:23:09
android中Glide实现加载图片保存至本地并加载回调监听
2021-12-05 23:00:03
C#子类对基类方法的继承、重写与隐藏详解
2023-01-31 04:48:46
如何让java只根据数据库表名自动生成实体类
2022-02-24 04:25:52
java金钱处理方法实例详解
2023-05-29 10:38:29
21天学习android开发教程之SurfaceView
2023-04-17 17:01:56