时间:2021-05-20
本文实例讲述了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#程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例为大家分享了C#异步调用的具体代码,供大家参考,具体内容如下usingSystem;usingSystem.Collections.Generic;us
使用@Async实现异步调用什么是”异步调用”与”同步调用”“同步调用”就是程序按照一定的顺序依次执行,,每一行程序代码必须等上一行代码执行完毕才能执行;”异步
异步调用在解释异步调用之前,我们先来看同步调用的定义;同步就是整个处理过程顺序执行,当各个过程都执行完毕,并返回结果。异步调用则是只是发送了调用的指令,调用者无
本文实例形式展示了C#中异步调用的实现方法,并对其原理进行了较为深入的分析,现以教程的方式分享给大家供大家参考之用。具体如下:首先我们来看一个简单的例子:小明在
本文实例讲述了C#异步委托调用实现方法。分享给大家供大家参考。具体如下:staticvoidMain(string[]args){//委托异步Actionsho