1、异步操作类
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static System.Windows.Forms.AxHost;namespace EapWindowApp
{internal class AsyncClass{//进度委托事件public delegate void ProgressChangedEventHandler(ProgressChangedEventArgs e);public event ProgressChangedEventHandler ProgressChanged;//完成委托事件public delegate void OperatingCompletedEventHandler(object sender,AsyncCompletedEventArgs e);public event OperatingCompletedEventHandler OperatingCompleted;//异步委托private delegate void OperatingHandler(AsyncOperation asyncOperation);private SendOrPostCallback OnProgressChangedDelegate;private SendOrPostCallback OnOperatingCompletedDelegate;/// <summary>/// 构造函数/// </summary>public AsyncClass() {OnProgressChangedDelegate = new SendOrPostCallback(OnProgressChanged);OnOperatingCompletedDelegate = new SendOrPostCallback(OnOperatingCompleted);}/// <summary>/// 进度事件改变处理/// </summary>/// <param name="state"></param>private void OnProgressChanged(object state){if(ProgressChanged != null){ProgressChangedEventArgs e =state as ProgressChangedEventArgs;ProgressChanged(e);}}/// <summary>/// 完成事件操作处理/// </summary>/// <param name="state"></param>private void OnOperatingCompleted(object state) { if(OperatingCompleted != null){AsyncCompletedEventArgs e = state as AsyncCompletedEventArgs;OperatingCompleted(this,e);}}/// <summary>/// 异步操作/// </summary>public void AsyncOperation(){AsyncOperation asyncOperation = AsyncOperationManager.CreateOperation(null);OperatingHandler oh = new OperatingHandler(DoWork);oh.BeginInvoke(asyncOperation,new AsyncCallback(OperationCallbcack), asyncOperation);}/// <summary>/// 回调函数/// </summary>/// <param name="ar"></param>private void OperationCallbcack(IAsyncResult ar){AsyncResult art=(AsyncResult)ar;OperatingHandler oh=art.AsyncDelegate as OperatingHandler;oh.EndInvoke(ar);AsyncOperation ao=ar.AsyncState as AsyncOperation;ao.PostOperationCompleted(OnOperatingCompletedDelegate, new AsyncCompletedEventArgs(null, false, true));}/// <summary>/// 操作执行/// </summary>/// <param name="asyncOperation"></param>private void DoWork(AsyncOperation asyncOperation){for (int i = 0; i <= 100; i++){Thread.Sleep(1000);if (i % 10 == 0){asyncOperation.Post(OnProgressChangedDelegate, new ProgressChangedEventArgs(i, null));//OnProgressChanged(i);}}//OnOperatingCompleted(true);}}
}
2、应用程序调用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace EapWindowApp
{public partial class Form1 : Form{/// <summary>/// 构造函数/// </summary>public Form1(){InitializeComponent();}/// <summary>/// Button事件处理函数/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){AsyncClass asyncClass = new AsyncClass();asyncClass.ProgressChanged += AsyncClass_ProgressChanged;asyncClass.OperatingCompleted += AsyncClass_OperatingCompleted;listBox1.Items.Add($"EAP开始执行...");asyncClass.AsyncOperation();listBox1.Items.Add($"EAP执行完成");}/// <summary>/// 操作完成处理/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void AsyncClass_OperatingCompleted(object sender, AsyncCompletedEventArgs e){listBox1.Items.Add($"事件完成状态!{e.UserState}");}/// <summary>/// 操作进度处理/// </summary>/// <param name="e"></param>private void AsyncClass_ProgressChanged(ProgressChangedEventArgs e){listBox1.Items.Add($"事件进度:{e.ProgressPercentage}%");}}
}
3、运行结果