C#基于事件的异步模式实现实例

ops/2024/10/18 14:27:45/

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、运行结果
在这里插入图片描述


http://www.ppmy.cn/ops/56643.html

相关文章

基于ssm的图书管理系统的设计与实现

摘 要 在当今信息技术日新月异的时代背景下&#xff0c;图书管理领域正经历着深刻的变革&#xff0c;传统的管理模式已难以适应现代社会的快节奏和高要求&#xff0c;逐渐向数字化、智能化的方向演进。本论文聚焦于这一转变趋势&#xff0c;致力于设计并成功实现一个基于 SSM&…

C语言从头学31——与字符串变量相关的几个函数

strlen、strcpy、strcat、strcmp、sprintf这些函数都是与字符串相关的&#xff0c;除了sprintf是定义在stdio.h中外&#xff0c;其余几个都定义在string.h中&#xff0c;比较新的编译器版本stdio.h中已经含有string.h的内容&#xff0c;所以编程时不需要再包含string.h这个头文…

Java项目中,常用的SQL语句

常用的命令&#xff1a; 1.数据的增删改查 1.插入数据(进行注册&#xff09; 语法 1&#xff1a; --第一种&#xff1a; INSERT INTO 表名(列名 1,列名 2, …) ; insert into tablename(member1,member3) valuse(,); --第二种&#xff1a; INSERT INTO 表名 VALUES(值 1,值 …

MacOS和Windows中怎么安装Redis

希望文章能给到你启发和灵感&#xff5e; 如果觉得文章对你有帮助的话&#xff0c;点赞 关注 收藏 支持一下博主吧&#xff5e; 阅读指南 开篇说明一、基础环境说明1.1 硬件环境1.2 软件环境 二、MacOS中Redis的安装2.1 HomeBrew 安装&#xff08;推荐&#xff09;2.2 通过官方…

解决了一个java Bug:Exception in thread “main“ java.lang.NullPointerException

写代码&#xff0c;遇到了个问题。 很纳闷&#xff0c;跟着人家写的代码。只能去查资料。 赶紧去找&#xff0c;自己的代码 逆天&#xff0c;赶紧改&#xff01; 成功了&#xff01;&#xff01;&#xff01;

视频汇聚平台EasyCVR设备录像回看请求播放时间和实际时间对不上,是何原因?

安防监控EasyCVR视频汇聚平台可提供多协议&#xff08;RTSP/RTMP/国标GB28181/GAT1400/海康Ehome/大华/海康/宇视等SDK&#xff09;的设备接入、音视频采集、视频转码、处理、分发等服务&#xff0c;系统具备实时监控、云端录像、回看、告警、平台级联以及多视频流格式分发等视…

爬虫-浏览器自动化

什么是selenium selenium是浏览器自动化测试框架&#xff0c;原本用于网页测试。但到了爬虫领域&#xff0c;它又成为了爬虫的好帮手。有了 selenium&#xff0c;我们便不再需要判断网页数据加载的方式&#xff0c;只要让 selenium 自动控制浏览器&#xff0c;就像有双无形的手…

深入解析补天平台:白帽黑客的奖金激励机制

摘要 补天平台&#xff0c;作为中国领先的网络安全众测平台&#xff0c;通过其独特的奖金激励机制&#xff0c;成功吸引了众多白帽黑客投身于网络安全的建设之中。本文将深入探讨补天平台的运作模式、奖金分配规则&#xff0c;并分析如何利用该平台提升个人网络安全技能。 1.…