进程间通信(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;
    };

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值