当前位置: 首页 > backend >正文

进程间通信(Windows事件)

C++/CLI程序

		//定义static HANDLE hEventVideo;//构造函数中初始化hEventVideo = NULL;// 初始化事件对象void InitializeEvent() {hEventVideo = CreateEvent(NULL,               // 默认安全属性TRUE,               // 手动重置事件FALSE,              // 初始无信号状态L"Global\\MyAppEvent" // 全局命名事件);if (hEventVideo == NULL) {throw gcnew Exception("事件创建失败");}hEventAnalysis = CreateEvent(NULL,               // 默认安全属性TRUE,               // 手动重置事件FALSE,              // 初始无信号状态L"Global\\MyApphEventAnalysis" // 全局命名事件);if (hEventAnalysis == NULL) {throw gcnew Exception("事件创建失败");}}//发送事件SetEvent(hEventVideo);//事件监听线程操作// 启动事件监听线程void StartEventThread() {isRunning = true;eventThread = gcnew Thread(gcnew ThreadStart(this, &Form1::EventMonitorProc));eventThread->IsBackground = true;eventThread->Start();}void EventMonitorProc() {HANDLE handles[3] = { hEvent, hEventAnalysis,hShutdownEvent };while (isRunning) {DWORD waitResult = WaitForMultipleObjects(3, handles, FALSE, INFINITE);if (waitResult == WAIT_OBJECT_0 + 0) {  // hEvent 被触发if (isRunning) { this->Invoke(gcnew Action(this, &Form1::ShowNotification));}ResetEvent(hEvent);  // 手动重置事件状态}else if (waitResult == WAIT_OBJECT_0 + 1) {  // hEventAnalysis 被触发if (isRunning) { this->Invoke(gcnew Action(this, &Form1::ShowNotificationAnalysisVedio));}ResetEvent(hEventAnalysis);  // 手动重置事件状态}else {// 处理其他返回值(如 WAIT_FAILED)// 可以在这里添加日志}}}void ShowNotification(){btnStartStopVedio->Focus();btnStartStopVedio->PerformClick();}//析构函数isRunning = false;  // 停止线程if (hShutdownEvent != NULL) {SetEvent(hShutdownEvent);  // 通知线程退出}if (eventThread != nullptr && eventThread->IsAlive) {eventThread->Join();  // 等待线程完全退出}if (hEvent != NULL) {CloseHandle(hEvent);  // 关闭事件句柄}if (hEventAnalysis != NULL){CloseHandle(hEventAnalysis);}if (hShutdownEvent != NULL) {CloseHandle(hShutdownEvent);  // 关闭关闭事件句柄}

完整winform例程

#pragma once
#include <Windows.h>
#include <vcclr.h>namespace IPC {using namespace System;using namespace System::Windows::Forms;using namespace System::Runtime::InteropServices;using namespace System::Threading;public ref class MyForm1 : public Form {private:static HANDLE hEvent;  // 事件对象句柄// 事件监听线程bool isRunning;  // 线程控制标志Thread^ eventThread;//测试了应该是不用//[DllImport("user32.dll", CharSet = CharSet::Unicode)]//static LRESULT DefWindowProcW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);//static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {//    return DefWindowProc(hWnd, msg, wParam, lParam);//}public:MyForm1(void) {hEvent = NULL;InitializeComponent();InitializeEvent();StartEventThread();}~MyForm1() {isRunning = false;SetEvent(hEvent);  // 唤醒线程退出if (eventThread != nullptr && eventThread->IsAlive) {eventThread->Join();}CloseHandle(hEvent);}private:void InitializeComponent() {this->button1 = gcnew Button();this->SuspendLayout();// 按钮配置this->button1->Location = Drawing::Point(124, 69);this->button1->Name = L"buttonBroadcast";this->button1->Text = L"广播事件";this->button1->Click += gcnew EventHandler(this, &MyForm1::OnButtonClick);this->ClientSize = Drawing::Size(573, 441);this->Controls->Add(this->button1);this->Name = L"MyForm1";this->Text = L"多实例事件示例";this->ResumeLayout(false);}// 初始化事件对象void InitializeEvent() {hEvent = CreateEvent(NULL,               // 默认安全属性TRUE,               // 手动重置事件[6](@ref)FALSE,              // 初始无信号状态[6](@ref)L"Global\\MyAppEvent" // 全局命名事件[6](@ref));if (hEvent == NULL) {throw gcnew Exception("事件创建失败");}}、、、、、、、、、、、、、、、、// 启动事件监听线程void StartEventThread() {isRunning = true;eventThread = gcnew Thread(gcnew ThreadStart(this, &MyForm1::EventMonitorProc));eventThread->IsBackground = true;eventThread->Start();}// 事件监听线程过程void EventMonitorProc() {while (isRunning) {DWORD waitResult = WaitForSingleObject(hEvent, INFINITE);  // 阻塞等待[6,9]if (waitResult == WAIT_OBJECT_0) {if (isRunning) {  // 排除关闭时的触发this->Invoke(gcnew Action(this, &MyForm1::ShowNotification));}ResetEvent(hEvent);  // 手动重置事件状态}}}// UI更新方法void ShowNotification() {MessageBox::Show("收到其他实例的事件通知!", "提示",MessageBoxButtons::OK, MessageBoxIcon::Information);}// 按钮点击事件处理System::Void OnButtonClick(System::Object^ sender, System::EventArgs^ e) {SetEvent(hEvent);}// 控件声明Button^ button1;};}
http://www.xdnf.cn/news/6207.html

相关文章:

  • 【教程】Docker方式本地部署Overleaf
  • 内存划分包括 Flash存储器、SRAM 和 外设寄存器
  • nginx 出现大量connect reset by peer
  • 第二章日志分析-apache日志分析
  • 秒删node_modules[无废话版]
  • 数据结构(八)——查找
  • 达梦数据库 【-6111: 字符串转换出错】问题处理
  • HVV蓝队实战面试题
  • 全新开发-iVX图形化编程VS完整IDE
  • 有关多线程
  • vue中,created和mounted两个钩子之间调用时差值受什么影响
  • Ubuntu摄像头打开失败
  • 16S18S_OTU分析(3)
  • 正则表达式(二)-高级应用_谨慎使用
  • Spark之搭建Yarn模式
  • 日本动漫风格人像街拍Lr调色预设,手机滤镜PS+Lightroom预设下载!
  • 【PostgreSQL数据分析实战:从数据清洗到可视化全流程】附录-D. 扩展插件列表(PostGIS/PostgREST等)
  • 搭建Caffeine+Redis多级缓存机制
  • ChatGPT 能“记住上文”的原因
  • nputop:昇腾 NPU 交互式监控工具
  • 基于 NanoDet 的工厂巡检机器人目标识别系统研究与实现​
  • Fluent Bit持久化配置:保障数据可靠传输的关键
  • MVCC:数据库并发控制的利器
  • 【计算机哲学故事1-5】版本更新:拒绝停滞,成长是最好的修复
  • 部署GraphRAG配置Neo4j实现知识图谱可视化【踩坑经历】
  • 【SSL证书系列】https双向认证中客户端认证的原理
  • 边缘计算网关工业物联网应用:空压机远程运维监控管理
  • 自动化 NuGet 包打包与上传:完整批处理脚本详解(含 SVN 支持)
  • MySQL基础原理
  • python之Pandas合并数据终极指南:pd.concat参数详解