[英]Winapi synchronizing processes
I have 3 simple program and each is a simple window. 我有3个简单的程序,每个都是一个简单的窗口。 I will start all 3 of the process and then click on program 1 or 2's button to show up the window of program 3.
我将启动所有3个过程,然后单击程序1或2的按钮以显示程序3的窗口。
Program 1 & 2: Only have 1 button. 程序1和2:只有1个按钮。 When clicked, shows the hidden process of program 3(which is also a window).
单击时,显示程序3的隐藏进程(也是一个窗口)。
Program 3: Starts up as a hidden process, and it is waiting for program 1 and 2's message before popping up. 程序3:作为隐藏进程启动,它在弹出之前等待程序1和2的消息。 Depending on the button press, the window caption should change to the caption of program 1 or 2.
根据按下按钮,窗口标题应更改为程序1或2的标题。
I am not sure what function or do I use a thread to make this behavior? 我不确定是什么函数或者我使用线程来做这个行为? I believe I need to use some sort of thread to do this.. first make program 3 hidden and then waiting for the message of program 1 and 2.. any ideas?
我相信我需要使用某种线程来做到这一点。首先让程序3隐藏,然后等待程序1和2的消息..任何想法?
EDIT: I am using C++ and I am told to use a semaphore. 编辑:我正在使用C ++,我被告知使用信号量。
Since you need to communicate a simple message across a process boundary, I'd recommend using something in the SendMessage family. 由于您需要跨进程边界传递简单消息,因此我建议在SendMessage系列中使用某些内容。 You will first need to acquire a handle for the target window.
您首先需要获取目标窗口的句柄。 This function is pretty low-level in the windowing API, and so you'll only be able to get at it directly from C/C++, but you didn't specify what language you were using, and I think there are wrappers around this routine for the CLR accessible through C# as well.
这个函数在窗口API中是相当低级的,所以你只能直接从C / C ++中获取它,但是你没有指定你使用的是什么语言,我认为有这个包装CLR的例程也可以通过C#访问。
I would suggest using a Windows Event . 我建议使用Windows事件 。 Specifically, a Manual Reset Event.
特别是手动重置事件。 Your program 3 does a Wait on the event.
你的程序3做了一个等待事件。 When Program 1 or Program 2 wants to wake the window up, it sets the event.
当程序1或程序2想要唤醒窗口时,它会设置事件。 When Program 3 goes back to hiding, it clears the event.
当程序3返回隐藏时,它会清除事件。
You could use SendMessage
or PostMessage
, but the event seems much easier and straightforward. 您可以使用
SendMessage
或PostMessage
,但事件似乎更容易和直接。 It also has certain advantages: 它还具有一定的优势:
This isn't an appropriate use for a semaphore. 这不适合信号量。 A semaphore is typically used to synchronize access to multiple shared resources.
信号量通常用于同步对多个共享资源的访问。 All you want here is for Program 3 to wait for a notification, and for Program 1 or Program 2 to be able to send that notification.
您只需要程序3等待通知,程序1或程序2就可以发送该通知。
If you have to pass data from Program 1 to Program 3, then the Event won't help you do that. 如果您必须将数据从程序1传递到程序3,那么事件将无法帮助您这样做。 You'll have to come up with a communication method such as a memory mapped file, pipe, network socket ... or even a Windows message in that case.
在这种情况下,您将不得不提出一种通信方法,例如内存映射文件,管道,网络套接字......甚至是Windows消息。 But for simple "Hey, wake up!"
但对于简单的“嘿,醒醒!” notification, I'd use an event.
通知,我会使用一个事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.